Can someone clarify what type of object is returned from a find (:select => ...) statement that only selects a subset of the records columns? If I have an object Foo with attributes A and B, and call find(:select => "A"), is the object that''s returned still a Foo object? Is it a new Foo object? Does it not have a B attribute (which would mean there would exist more than one type for Foo)? or is the B attribute simply nil? Is there a way to replicate the behavior of the :select on an object - to return an object that appears to be the same type but has a subset of the attributes? Thank you, Andrew -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 3, 9:27 am, acreadinglist <andrew.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can someone clarify what type of object is returned from a find > (:select => ...) statement that only selects a subset of the records > columns?Foo.find always returns instances of Foo. That instance may have extra attributes, it may have fewer attributes or the attributes may have values other than what they would have if you had just done Foo.find (1). Not sure what you mean by multiple types of Foo Fred> > If I have an object Foo with attributes A and B, and call find(:select > => "A"), is the object that''s returned still a Foo object? Is it a > new Foo object? Does it not have a B attribute (which would mean > there would exist more than one type for Foo)? or is the B attribute > simply nil? > > Is there a way to replicate the behavior of the :select on an object - > to return an object that appears to be the same type but has a subset > of the attributes? > > Thank you, > Andrew-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Jan 3, 2010 at 4:27 AM, acreadinglist <andrew.cove-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can someone clarify what type of object is returned from a find > (:select => ...) statement that only selects a subset of the records > columns? > > If I have an object Foo with attributes A and B, and call find(:select > => "A"), is the object that''s returned still a Foo object? Is it a > new Foo object? Does it not have a B attribute (which would mean > there would exist more than one type for Foo)?First of all, assuming that you mean class for type here, presumably because you are coming from a language like Java or C++, Ruby doesn''t work the same way. In Ruby different instances of the same class can have different sets of instance variables. Instance variables aren''t declared in the class definition, an object acquires instance variables when they are referenced in instance methods, and those instance methods can come from modules as well as classes, so if we have class Foo def m @iv1 = 1 end def n @iv2 = 2 end end foo1 = Foo.new foo1.m foo2 = Foo.new foo2.n foo3 = Foo.new At this point foo1 will have @iv1, but not @iv2, foo2 will have @iv2 but not iv1, and foo3 will have neither instance variable. Second, in the case of ActiveRecord attributes, theses aren''t direct instance variables at all. Instead an instance ActiveRecord::Base or one of its subclasses has an @attributes instance variable which contains a hash from attribute names to attribute values. The accessor methods for models are dynamically generated and will generate errors as appropriate, for example lets say you have a model Foo with attributes, first_name, and last_name, and do f = Foo.first(:select => ''first_name'') f.last_name will raise an error: ActiveRecord::MissingAttributeError: missing attribute: last_name This may seem strange to someone accustomed to statically typed systems, but it actually works rather well in practice. It just might take some getting used to. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Jan-04 06:21 UTC
Re: behavior of find(:select => ...)
Super explanation and thank you very much On Jan 3, 8:46 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Jan 3, 2010 at 4:27 AM, acreadinglist <andrew.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Can someone clarify what type of object is returned from a find > > (:select => ...) statement that only selects a subset of the records > > columns? > > > If I have an object Foo with attributes A and B, and call find(:select > > => "A"), is the object that''s returned still a Foo object? Is it a > > new Foo object? Does it not have a B attribute (which would mean > > there would exist more than one type for Foo)? > > First of all, assuming that you mean class for type here, presumably > because you are coming from a language like Java or C++, Ruby doesn''t > work the same way. > > In Ruby different instances of the same class can have different sets > of instance variables. Instance variables aren''t declared in the > class definition, an object acquires instance variables when they are > referenced in instance methods, and those instance methods can come > from modules as well as classes, so if we have > > class Foo > def m > @iv1 = 1 > end > def n > @iv2 = 2 > end > end > > foo1 = Foo.new > foo1.m > foo2 = Foo.new > foo2.n > foo3 = Foo.new > > At this point foo1 will have @iv1, but not @iv2, foo2 will have @iv2 > but not iv1, and foo3 will have neither instance variable. > > Second, in the case of ActiveRecord attributes, theses aren''t direct > instance variables at all. Instead an instance ActiveRecord::Base or > one of its subclasses has an @attributes instance variable which > contains a hash from attribute names to attribute values. The accessor > methods for models are dynamically generated and will generate errors > as appropriate, for example lets say you have a model Foo with > attributes, first_name, and last_name, and do > > f = Foo.first(:select => ''first_name'') > f.last_name > > will raise an error: > ActiveRecord::MissingAttributeError: missing attribute: last_name > > This may seem strange to someone accustomed to statically typed > systems, but it actually works rather well in practice. It just might > take some getting used to. > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Twitter:http://twitter.com/RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks. That makes sense. Much appreciated! On Jan 3, 7:46 am, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Jan 3, 2010 at 4:27 AM, acreadinglist <andrew.c...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Can someone clarify what type of object is returned from a find > > (:select => ...) statement that only selects a subset of the records > > columns? > > > If I have an object Foo with attributes A and B, and call find(:select > > => "A"), is the object that''s returned still a Foo object? Is it a > > new Foo object? Does it not have a B attribute (which would mean > > there would exist more than one type for Foo)? > > First of all, assuming that you mean class for type here, presumably > because you are coming from a language like Java or C++, Ruby doesn''t > work the same way. > > In Ruby different instances of the same class can have different sets > of instance variables. Instance variables aren''t declared in the > class definition, an object acquires instance variables when they are > referenced in instance methods, and those instance methods can come > from modules as well as classes, so if we have > > class Foo > def m > @iv1 = 1 > end > def n > @iv2 = 2 > end > end > > foo1 = Foo.new > foo1.m > foo2 = Foo.new > foo2.n > foo3 = Foo.new > > At this point foo1 will have @iv1, but not @iv2, foo2 will have @iv2 > but not iv1, and foo3 will have neither instance variable. > > Second, in the case of ActiveRecord attributes, theses aren''t direct > instance variables at all. Instead an instance ActiveRecord::Base or > one of its subclasses has an @attributes instance variable which > contains a hash from attribute names to attribute values. The accessor > methods for models are dynamically generated and will generate errors > as appropriate, for example lets say you have a model Foo with > attributes, first_name, and last_name, and do > > f = Foo.first(:select => ''first_name'') > f.last_name > > will raise an error: > ActiveRecord::MissingAttributeError: missing attribute: last_name > > This may seem strange to someone accustomed to statically typed > systems, but it actually works rather well in practice. It just might > take some getting used to. > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Twitter:http://twitter.com/RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.