Forum: Ruby on Rails Navigation between DB Items with active Record

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (alain)
on 2006-05-20 02:44
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!
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-05-20 08:48
(Received via mailing list)
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
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (alain)
on 2006-05-20 15:34
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!
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-05-20 16:01
(Received via mailing list)
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
This topic is locked and can not be replied to.