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