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!
on 2006-05-20 04:44
on 2006-05-20 10:48
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
on 2006-05-20 17: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.concept and do an assert_not_nil to make sure it exist. Thanks again!
on 2006-05-20 18:01
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