新手在这里铁路.我正在创建一个应用来管理用户,文本片段,片段评论和喜欢.
每个用户都有许多片段,并有很多评论.每个片段属于某个用户,有很多评论,并且可以有很多喜欢.每个评论属于某个用户,属于一个片段,并且可以有很多喜欢.
我的问题是Like模型.A like属于某个用户,属于EITHER片段或注释.
我的迁移看起来像这样:
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.references :user
t.references :snippet # or :comment...?
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的代码段模型:
class Snippet < ActiveRecord::Base
has_many :comments
has_many :likes
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我的评论模型:
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :snippet
has_many :likes
end
Run Code Online (Sandbox Code Playgroud)
我喜欢的模特:
class Like < ActiveRecord::Base
belongs_to: :user
# belongs to either :snippet or :comment, depending on
# where the like is, and what controller it was submitted to
end
Run Code Online (Sandbox Code Playgroud)
我不能同时引用它们,我觉得创建一种新类型的Like对于应用程序中的每种内容都会很混乱. …