Say I have the following models:
class Parent < ActiveRecord::Base
has_one :child
end
class Child < ActiveRecord::Base
belongs_to :parent
end
I'd like to retrive the parent through the child, but doing the following fails: I find the model in the following way through a controller
@child = Child.find(params[:child_id])
(Not sure if this is relevant, but since I'm using shallow routing, the parent_id is not available in the URL)
In my view, I'd like to retrieve the child's parent like this:
@child.parent
How would I go about doing this?
Thanks!
Update: my example (when I decided to start a new app and create it) actually ran perfectly.
In my actual app, I forgot to include belongs_to :parent
in the child's model. How silly of me. Thanks for taking the time to comment and answer, guys. Next time I'll look more carefully before posting a question here.