Shpigford
Shpigford

Reputation: 25338

Custom has_many?

I have a Rule model that references a Question model using a question_id field.

The rule belongs_to question and the question has_many rules.

So, I can do rule.question.name.

But, there is also a show_question_id field. It should also reference the Question model, but that's what I can't figure out.

I'd like to be able to do something like rule.show_question.name.

Upvotes: 0

Views: 211

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

belongs_to :show_question, :class_name => 'Question'

Rails infers the foreign key and the class name from the name of the association, so you need to tell it to use Question rather than the (non-existent) ShowQuestion

Upvotes: 3

Related Questions