# Behavior of find(:select =\> ...)

**URL:** <https://www.ruby-forum.com/t/behavior-of-find-select/180995>\
**Category:** Rails\
**Created:** [January 3, 2010, 11:48am UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995 "2010-01-03T11:48:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 3, 2010, 11:48am UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995/1 "2010-01-03T11:48:26Z")

</div>

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 3, 2010, 11:51am UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995/2 "2010-01-03T11:51:57Z")

</div>

On Jan 3, 9:27 am, acreadinglist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

> 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 3, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995/3 "2010-01-03T17:29:07Z")

</div>

On Sun, Jan 3, 2010 at 4:27 AM, acreadinglist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 4, 2010, 7:23am UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995/4 "2010-01-04T07:23:12Z")

</div>

Super explanation and thank you very much

On Jan 3, 8:46 pm, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> First of all, assuming that you mean class for type here, presumably  
> def m  
> foo2.n  
> as appropriate, for example lets say you have a model Foo with  
> take some getting used to.
> 
> –  
> Rick DeNatale
> 
> Blog:[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter:[http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR:[http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn:[Rick DeNatale - Developer - IBM | 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 4, 2010, 9:02am UTC](https://www.ruby-forum.com/t/behavior-of-find-select/180995/5 "2010-01-04T09:02:08Z")

</div>

Thanks. That makes sense. Much appreciated!

On Jan 3, 7:46 am, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> First of all, assuming that you mean class for type here, presumably  
> def m  
> foo2.n  
> as appropriate, for example lets say you have a model Foo with  
> take some getting used to.
> 
> –  
> Rick DeNatale
> 
> Blog:[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter:[http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR:[http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn:[Rick DeNatale - Developer - IBM | 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
