views:

31

answers:

1

Hi guys,

I'm having a problem with some associations in Rails3.

I have the following associations

InformationCategory :has_many => Informations :has_many => InformationValues

I'm able to succesfully do the following:

Information.first.information_values
=> [#InformationValue..]

I'm also able to do the following:

InformationCategory.first.informations
=> [#Information...]

However, for some reason, this fails:

InformationCategory.first.informations.first.information_values
=> NoMethodError: undefined method `information_values' for #<Information:0x000001053321c8>

Why can't I use 'nested associations' in Rails? The error message clearly states that InformationCategory.first.informations.first returns an instance of Information

Am I doing something wrong?

Thanks in advance!

A: 

You don't define all the nested descendants on outermost model: each model defines what it "directly" has_many of, or the model it belongs_to. Because your question is wrong, I can only guess without seeing more specifically how your models are supposed to be related.

This might be a start:

class InformationCategory < ActiveRecord::Base
  has_many :informations
end

class Information < ActiveRecord::Base
  belongs_to :information_category
  has_many :information_values
end

class InformationValue < ActiveRecord::Base
  belongs_to :information
end

However, you might be trying to do a has_many :through, but I can't tell from your question.

Andrew Vit