0
我有3个模型:黑客,投票,用户。如何正确建立我的模型关系
用户可以创建很多黑客。 每个用户都应该能够对每个黑客进行一次投票(评级为1-5。如果发生错误或其他情况,评级应该是可更新的)。
我想到了以下关系: Hack.rb
belongs_to :user
User.rb
has_many :hacks
Votes.rb
belongs_to :user
belongs_to :hack
那是正确的还是我失去了一些东西? 我想过让所有的票这样以后:
Hack.first.votes
什么样的外国键做我必须设置的? 在我的schema.rb中,我已经成功地将我的用户设置为< => hack关系,没有任何外键。
ActiveRecord::Schema.define(version: 20141019161631) do
create_table "hacks", force: true do |t|
t.string "url"
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "email", null: false
t.string "crypted_password", null: false
t.string "salt", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "role"
end
end
非常感谢您提前!
非常感谢。这些关系听起来合理。你知道Rails是否要求我手动设置这些外部列,或者如果belongs_to/has_many roptions为我做了这些吗? – 2014-10-19 20:03:26
我发现了一个错误。用你的答案黑客has_many用户,但一个黑客应该只分配给一个用户。一个用户,但是,可以创建很多黑客。这意味着 用户has_many:黑客。 用户has_many:票数 黑客belongs_to:用户 黑客has_many:票数通过:用户 – 2014-10-19 20:51:51