0
在我的應用程序,一個Product
有許多subscriptions
與用戶是User
:僅當關聯存在時才顯示鏈接?
class User
has_many :products
has_many :subscriptions, :foreign_key => :subscriber_id
end
class Product
belongs_to :store
has_many :subscriptions, :as => :subscribable
end
class Subscription
belongs_to :subscriber, :class_name => "User"
belongs_to :subscribable, :polymorphic => true
end
我怎麼會顯示是否該產品已訂閱或不基於鏈接塊?
<% if #@product.subscription.present? %>
<%= link_to "Unsubscribe", { :controller => "products", :action => "unsubscribe_product", :id => product.id }, :method => :delete %>
<% else %>
<%= link_to "Subscribe", { :controller => "products", :action => "subscribe_product", :id => product.id }, :method => :post %>
<% end %>