在我的应用程序中, a与订阅者Product
有很多是 a :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 %>