Message from discussion
ActiveRecord and [] accessors
Received: by 10.224.97.134 with SMTP id l6mr3617053qan.6.1333725908092;
Fri, 06 Apr 2012 08:25:08 -0700 (PDT)
X-BeenThere: rubyonrails-core@googlegroups.com
Received: by 10.224.53.12 with SMTP id k12ls3903119qag.5.gmail; Fri, 06 Apr
2012 08:25:00 -0700 (PDT)
Received: by 10.224.173.70 with SMTP id o6mr3638774qaz.2.1333725900664;
Fri, 06 Apr 2012 08:25:00 -0700 (PDT)
Received: by 10.224.173.70 with SMTP id o6mr3638772qaz.2.1333725900646;
Fri, 06 Apr 2012 08:25:00 -0700 (PDT)
Return-Path: <k...@actionmoniker.com>
Received: from mail-qa0-f52.google.com (mail-qa0-f52.google.com [209.85.216.52])
by gmr-mx.google.com with ESMTPS id c25si1850273qch.2.2012.04.06.08.25.00
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 06 Apr 2012 08:25:00 -0700 (PDT)
Received-SPF: pass (google.com: domain of k...@actionmoniker.com designates 209.85.216.52 as permitted sender) client-ip=209.85.216.52;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of k...@actionmoniker.com designates 209.85.216.52 as permitted sender) smtp.mail=...@actionmoniker.com
Received: by qabg40 with SMTP id g40so411310qab.4
for <rubyonrails-core@googlegroups.com>; Fri, 06 Apr 2012 08:25:00 -0700 (PDT)
d=google.com; s=20120113;
h=subject:references:from:content-type:x-mailer:in-reply-to
:message-id:date:to:content-transfer-encoding:mime-version
:x-gm-message-state;
bh=8uOcyezqy617Q3rerxRSFSAOGyRE55/Yp6lle1tpRP0=;
b=iBRuOsHhqQqnhakeNHG/d6MLyypvUNs5B9E4VX5BIm8f4xOv8L6yXbu6sYSEU9gi7j
MhO2b2UUNP0S+CVYsRxLpQESLIyjZDJq32kB/o2AIy0H+V5SXXnetpx1D5NdnqPuo/Ps
4Ww8yOj1iWDbcgkqirR9NjtBb7Ejf1wB4xrl1cI70R4DsMPv1Jzcx4JM9OYKHH+0JXw8
l3i76AfH5RrBNWbMC51tPJV2bJJQqdUTQpLsqT/fHC04yAo3xpkiZTjOVRXMRpWUERJi
nvAOLTAi6x91uVttIOMLSwuSn51vYOnTVtm5kujGNqHPvcE0V7gqWJ0oQv3F7yE/HPY2
H1mQ==
Received: by 10.224.101.72 with SMTP id b8mr9669552qao.53.1333725900334;
Fri, 06 Apr 2012 08:25:00 -0700 (PDT)
Return-Path: <k...@actionmoniker.com>
Received: from [172.20.10.7] (73.sub-174-252-115.myvzw.com. [174.252.115.73])
by mx.google.com with ESMTPS id dm8sm11313626qab.18.2012.04.06.08.24.57
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 06 Apr 2012 08:24:58 -0700 (PDT)
Subject: Re: [Rails-core] ActiveRecord and [] accessors
References: <4F7F0218.70...@gmail.com>
From: Ken Collins <k...@actionmoniker.com>
Content-Type: multipart/alternative;
boundary=Apple-Mail-3F8870C6-145D-42C7-ACA1-9428EFF8581C
X-Mailer: iPad Mail (9B176)
In-Reply-To: <4F7F0218.70...@gmail.com>
Message-Id: <9BF96CB2-5899-4EDD-8C23-05E183F29...@actionmoniker.com>
Date: Fri, 6 Apr 2012 11:24:54 -0400
To: "rubyonrails-core@googlegroups.com" <rubyonrails-core@googlegroups.com>
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (1.0)
X-Gm-Message-State: ALoCoQnsMGH1fkhjpUYNpydychKkJEYjiK89b4PS1o6kmjkK2sjiz5llUnOIJweJZuGjb459+Kq0
--Apple-Mail-3F8870C6-145D-42C7-ACA1-9428EFF8581C
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
They are established aliases to read and write attribute. I know changing wo=
uld break a ton of my own code and various gems I author.=20
Sent from my iPad
On Apr 6, 2012, at 10:47 AM, Rodrigo Rosenfeld Rosas <rr.ro...@gmail.com> wr=
ote:
> Currently [] and []=3D are defined as follows for ActiveRecord instances:
>=20
> https://github.com/rails/rails/blob/master/activerecord/lib/active_record/=
attribute_methods.rb
>=20
> def [](attr_name)
> read_attribute(attr_name)
> end
>=20
> def []=3D(attr_name, value)
> write_attribute(attr_name, value)
> end
>=20
> If those accessors are used then overridden attribute accessors wouldn't b=
e called.
>=20
> I didn't find any references about the []/[]=3D methods in the current API=
, so I just mentioned them in docrails:
>=20
> https://github.com/lifo/docrails/commit/a49fe6ec2cb9f22a3b18d7db5ae2ee9032=
5e9d09
>=20
> But then I realized that the given example may have undesired side effects=
:
>=20
> [:deleted, :disabled].each{|p| model[p] =3D params[p] =3D=3D 'true' }
>=20
> What if "deleted=3D" or "disabled=3D" were overridden? They wouldn't be ca=
lled.
>=20
> So I was thinking that maybe Rails 4 could change their implementation to:=
>=20
> def [](attr_name)
> send attr_name.to_s
> end
>=20
> def []=3D(attr_name, value)
> send :"#{attr_name}=3D", value
> end
>=20
> Better to read this style of code in some framework than inside some appli=
cation...
>=20
> Any thoughts?
>=20
> Happy Easter!
>=20
> Rodrigo.
>=20
> --=20
> You received this message because you are subscribed to the Google Groups "=
Ruby on Rails: Core" group.
> To post to this group, send email to rubyonrails-core@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-core+unsubscribe=
@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyo=
nrails-core?hl=3Den.
--Apple-Mail-3F8870C6-145D-42C7-ACA1-9428EFF8581C
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
charset=utf-8
<html><head></head><body bgcolor="#FFFFFF"><div>They are established aliases to read and write attribute. I know changing would break a ton of my own code and various gems I author. <br><br>Sent from my iPad</div><div><br>On Apr 6, 2012, at 10:47 AM, Rodrigo Rosenfeld Rosas <<a href="mailto:rr.ro...@gmail.com">rr.ro...@gmail.com</a>> wrote:<br><br></div><div></div><blockquote type="cite"><div>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
Currently [] and []= are defined as follows for ActiveRecord
instances:<br>
<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb">https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods.rb</a><br>
<br>
def [](attr_name)<br>
read_attribute(attr_name)<br>
end<br>
<br>
def []=(attr_name, value)<br>
write_attribute(attr_name, value)<br>
end<br>
<br>
If those accessors are used then overridden attribute accessors
wouldn't be called.<br>
<br>
I didn't find any references about the []/[]= methods in the current
API, so I just mentioned them in docrails:<br>
<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a href="https://github.com/lifo/docrails/commit/a49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09">https://github.com/lifo/docrails/commit/a49fe6ec2cb9f22a3b18d7db5ae2ee90325e9d09</a><br>
<br>
But then I realized that the given example may have undesired side
effects:<br>
<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
[:deleted, :disabled].each{|p| model[p] = params[p] == 'true' }<br>
<br>
What if "deleted=" or "disabled=" were overridden? They wouldn't be
called.<br>
<br>
So I was thinking that maybe Rails 4 could change their
implementation to:<br>
<br>
def [](attr_name)<br>
send attr_name.to_s<br>
end<br>
<br>
def []=(attr_name, value)<br>
send :"#{attr_name}=", value<br>
end<br>
<br>
Better to read this style of code in some framework than inside some
application...<br>
<br>
Any thoughts?<br>
<br>
Happy Easter!<br>
<br>
Rodrigo.<br>
<br>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.<br>
To post to this group, send email to <a href="mailto:rubyonrails-core@googlegroups.com">rubyonrails-core@googlegroups.com</a>.<br>
To unsubscribe from this group, send email to <a href="mailto:rubyonrails-core+unsubscribe@googlegroups.com">rubyonrails-core+unsubscribe@googlegroups.com</a>.<br>
For more options, visit this group at <a href="http://groups.google.com/group/rubyonrails-core?hl=en">http://groups.google.com/group/rubyonrails-core?hl=en</a>.<br>
</div></blockquote></body></html>
--Apple-Mail-3F8870C6-145D-42C7-ACA1-9428EFF8581C--