【发布时间】:2014-06-26 22:26:23
【问题描述】:
为我的应用程序在 Rails 中建模“喜欢”的最佳方法是什么。我可以:
class User < ActiveRecord::Base
has_many :things
has_many :likes
has_many :liked_things, through: :likes, source: :thing
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
class Thing < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :liking_users, through: :likes, source: :user
end
或者
class User < ActiveRecord::Base
has_many :things
has_and_belongs_to_many :things
end
class Thing < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :users
end
哪种方法最好,为什么?如果这有助于确定最佳方法,我还计划在我的应用中添加活动源。
【问题讨论】:
标签: ruby-on-rails ruby activerecord social-networking