# Navigation between DB Items with active Record

**URL:** <https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104>\
**Category:** Rails\
**Created:** [May 20, 2006, 2:44am UTC](https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104 "2006-05-20T02:44:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [May 20, 2006, 2:44am UTC](https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104/1 "2006-05-20T02:44:05Z")

</div>

Hi,

I am having troubles to figure out how to move from one record to the  
other without using \<find\_by\_sql\> or other ugly tricks. So far, I have  
managed to do a lot of stuff without it but I decided that I need to  
learn how it works this weekend! So I need your help to figure that  
out…

I have the following 4 models:

class Client \< ActiveRecord::Base  
has\_many :client\_variations  
has\_many :variations, :through =\> :client\_variations  
end

class ClientVariation \< ActiveRecord::Base  
belongs\_to :client  
belongs\_to :variation  
end

class Variation \< ActiveRecord::Base  
belongs\_to :concept  
has\_many :client\_variations, :dependent =\> :destroy  
end

class Concept \< ActiveRecord::Base  
has\_many :variations, :order =\>“id”, :dependent =\> :destroy  
end

How can I get one of the Concept record associated with a specific  
Client? So far I have this:

temp = clients(:air\_canada).variations.concept.find(:all, limit =\> 1)

But I get this error:  
NoMethodError: undefined method `concept’ for Variation:Class

Any help would be great because I am tired to hacking AR by using SQL  
calls…

Thanks!

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [May 20, 2006, 8:48am UTC](https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104/2 "2006-05-20T08:48:55Z")

</div>

Alain Pilon wrote:

> class Client \< ActiveRecord::Base  
> belongs\_to :concept  
> temp = clients(:air\_canada).variations.concept.find(:all, limit =\> 1)
> 
> But I get this error:  
> NoMethodError: undefined method `concept’ for Variation:Class
> 
> Any help would be great because I am tired to hacking AR by using SQL  
> calls…
> 
> Thanks!

The above is failing (I think) because

clients(:air\_canada).variations

has (or could have) more than one result and therefore returns an array  
of Variation objects, and is equivalent to Variation.find(:all,  
:conditions =\> [#some condition]).concept

Are you trying to get all concepts that belong to variations owned by  
air\_canada clients? If so, I’m wondering if include might help here.  
Something (untested, but the console is your friend on these sorts of  
things) like:

temp = clients(:air\_canada).variations.find(:all, :include =\> :concept)

If you do this query in the console it’s dump the result on screen and  
you should then be able to see how to extract the concepts.

HTH

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [May 20, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104/3 "2006-05-20T15:34:55Z")

</div>

Thanks Chris,

you were right, the problem was caused by an array. I figure it out last  
night before going to bed and forgot it during the night until I saw  
your post 😉 Thanks!

Since I was only interested in getting the first Variation, I put:

first\_concept = clients(:air\_canada).variations[0].concept

and do an assert\_not\_nil to make sure it exist.

Thanks again!

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [May 20, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/navigation-between-db-items-with-active-record/61104/4 "2006-05-20T16:01:13Z")

</div>

Alain Pilon wrote:

> and do an assert\_not\_nil to make sure it exist.
> 
> Thanks again!

No prob. I had a similar thing earlier in the week where it became clear  
after a good night’s sleep
