0
我有以下情況,用戶可以創建新聞快遞,也可以訂閱新聞快遞。有沒有更好的方法來區分這兩種能力與關聯,或者我可以將所有權置於訂閱模式中,如multiple has_many associations in Rails所示。有了這個解決方案,我該如何編寫新聞稿模型以獲得新聞通訊所有者和新聞通訊訂閱者。Rails關聯 - 多種模型能力
Newsletter Model
# fields: id, user_id
has_many :subscriptions
has_many :users, through: :subscriptions
belongs_to :user *i.e. created by a single user*
Subscription Model
# fields: user_id, newsletter_id
belongs_to :newsletter
belongs_to :user
User Model
# fields: id
has_many :subscriptions
has_many :newsletters, through: :subscriptions
has_many :newsletters *i.e. created many newsletters*
has_many :created_newsletters, :through => :subscriptions, *old answer*
:source => :newsletter,
:conditions => ["newsletter.creator = ?", true]
是否也可以說user.newsletters和user.newsletter得到用戶和創造者? –
是的,如果這就是你想要的。但它的軌道約定有has_many協會作爲複數 – usha
謝謝!順便說一下,在這種形式下,新的「creator」/「subscribed_newsletters」模型的技術名稱是什麼?即我可以在多個模型中使用相同的名稱除了feed.creator的「belongs_to:creator,class_name:」Feed「,foreign_key::user_id」「以及newsletter.creator? –