# Get a collection through ActiveRecord for using with option\_

**URL:** <https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447>\
**Category:** Rails\
**Created:** [July 14, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447 "2006-07-14T20:35:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauloee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d9b06d/32.png) [@pauloee](https://www.ruby-forum.com/u/pauloee)\
**Post date:** [July 14, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447/1 "2006-07-14T20:35:07Z")

</div>

Hello,

I am having some difficulties with ActiveRecord.

What I want to do is to get a collection of SubjectGroups for using  
with option\_groups\_from\_collection\_for\_select

My Code:

========================================  
class Subject \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
belongs\_to :subject\_group  
end

class SubjectGroup \< ActiveRecord::Base  
has\_many :subjects  
end

@subjects= Subject.find(:all, :include =\> :subject\_group)  
@groups= SubjectGroup.find(:all, :include =\> :subjects)

The first find instruction runs just fine, the second gives me the  
following error:

You have a nil object when you didn’t expect it!, The error occurred  
while evaluating nil.loaded

I was expecting a SubjectGroup collection with all Subjects associated.

In both cases I saw the generated queries through logs and returns the  
same query result.

Thank you for your attention,

Paulo A.

---

<div class="post-metadata">

**Author:** ![pauloee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d9b06d/32.png) [@pauloee](https://www.ruby-forum.com/u/pauloee)\
**Post date:** [July 15, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447/2 "2006-07-15T15:24:40Z")

</div>

Paulo A. wrote:

> You have a nil object when you didn’t expect it!, The error occurred  
> while evaluating nil.loaded

Is that the entire code of your SubjectGroup model, or have you also  
defined your own “subjects” method within it?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![pauloee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d9b06d/32.png) [@pauloee](https://www.ruby-forum.com/u/pauloee)\
**Post date:** [July 15, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447/3 "2006-07-15T17:13:39Z")

</div>

On 7/15/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > following error:
> > 
> > You have a nil object when you didn’t expect it!, The error occurred  
> > while evaluating nil.loaded
> 
> Is that the entire code of your SubjectGroup model, or have you also  
> defined your own “subjects” method within it?

The idia is to add my own methods for  
option\_groups\_from\_collection\_for\_select, however I am not able to  
make this search. if I remove :include =\> :subjects it just works.

In the IRC channel someone told me that I have a Subject with a  
subject\_group\_id that refers to a subject\_group that doesnt exist, but  
I already verified that.

Paulo A.

---

<div class="post-metadata">

**Author:** ![pauloee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d9b06d/32.png) [@pauloee](https://www.ruby-forum.com/u/pauloee)\
**Post date:** [July 15, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447/4 "2006-07-15T17:17:07Z")

</div>

On 7/15/06, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > following error:
> > 
> > You have a nil object when you didn’t expect it!, The error occurred  
> > while evaluating nil.loaded
> 
> Is that the entire code of your SubjectGroup model, or have you also  
> defined your own “subjects” method within it?

Oh sorry, I did not answer to your question. Is the entire code, no  
methods added yet.

Paulo A.

---

<div class="post-metadata">

**Author:** ![pauloee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d9b06d/32.png) [@pauloee](https://www.ruby-forum.com/u/pauloee)\
**Post date:** [July 15, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/get-a-collection-through-activerecord-for-using-with-option/98447/5 "2006-07-15T18:58:14Z")

</div>

> Oh sorry, I did not answer to your question. Is the entire code, no  
> methods added yet.

Oh god, I think I need to sleep 😑 in fact I had two member functions,  
that was the problem.

Paulo A…
