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

**URL:** <https://discuss.rubyonrails.org/t/behavior-of-find-select/48319>\
**Category:** rubyonrails-talk\
**Created:** [January 3, 2010, 9:27am UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319 "2010-01-03T09:27:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_C](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Andrew\_C](https://discuss.rubyonrails.org/u/Andrew_C)\
**Post date:** [January 3, 2010, 9:27am UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319/1 "2010-01-03T09:27:38Z")

</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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 3, 2010, 10:50am UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319/2 "2010-01-03T10:50:46Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rick\_DeNatale](https://discuss.rubyonrails.org/u/Rick_DeNatale)\
**Post date:** [January 3, 2010, 3:46pm UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319/3 "2010-01-03T15:46:12Z")

</div>

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 &nbsp;&nbsp;&nbsp;def m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@iv1 = 1 &nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;def n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@iv2 = 2 &nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Mail\_from\_RAMU](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Mail\_from\_RAMU](https://discuss.rubyonrails.org/u/Mail_from_RAMU)\
**Post date:** [January 4, 2010, 6:21am UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319/4 "2010-01-04T06:21:33Z")

</div>

Super explanation and thank you very much

---

<div class="post-metadata">

**Author:** ![Andrew\_C](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Andrew\_C](https://discuss.rubyonrails.org/u/Andrew_C)\
**Post date:** [January 4, 2010, 8:00am UTC](https://discuss.rubyonrails.org/t/behavior-of-find-select/48319/5 "2010-01-04T08:00:33Z")

</div>

Thanks. That makes sense. Much appreciated!
