If I have a polymorphic association, how do I access related methods in my views?
For example, let’s say the model associations are:
class Order < ActiveRecord::Base
belongs_to :orderable, :polymorphic => true
end
class Product < ActiveRecord::Base
has_many :orders, :as => :orderable
end
And, in the Order view, I tried using:
<%= @order.product.id %>
But, that doesn’t work. How do you access related models in the views then?
EDIT: Here’s the form I’m using:
<% form_for [@orderable, @order] do |f| %>
...
<% end %>
And then, I’m including it in the Product Show view, like this:
<%= render 'orders/form' %>
Try this:
Use the name given to the
:asto access the parent:Also, here is some info in the Rails Guides