Rails 3 has_many :through 只是从关系的一侧添加 id
我正在尝试从 has_many、belongs_to 关系转变为 has_many :through 与我的项目和用户模型的关系。然而,当我在 Rails 控制台中测试它时,似乎只有关系的一侧在连接模型中设置了。
我目前的设置如下:
class Project < ActiveRecord::Base
has_many :collaborations
has_many :users, :through => :collaborations
end
class User < ActiveRecord::Base
has_many :collaborations
has_many :projects, :through => :collaborations
end
class Collaboration < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
然后在控制台中输入:
me = User.find(1)
p = Project.new(:name => "somename")
p.users << me
p.save
所发生的情况是,协作模型获取了 project_id 集,但没有获取 user_id。
当我执行以下操作时,会发生相反的情况(设置了 user_id 但未设置 project_id):
me.projects << p
基本上,它创建了一个“协作”,但仅保存了关系的一侧。
我已经被困在这个问题上几个小时了,我觉得这是一件小事——也许是我在这两个模型之间进行一对多关系时留下的东西。
- -编辑 - - 这就是架构的样子:
create_table "collaborations", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "shared", :default => false
t.integer "position", :default => 0
t.string "permalink"
t.text "description"
end
create_table "users", :force => true do |t|
t.string "username"
t.string "email"
t.string "crypted_password"
t.string "password_salt"
t.string "persistence_token"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.string "first_name"
t.string "last_name"
t.string "perishable_token", :default => "", :null => false
t.integer "avatar", :default => 0
end
任何帮助将不胜感激!
I am trying to move from a has_many, belongs_to relationship to a has_many :through relationship with my Project and User model. However when I test it in the rails console only one side of the relationship seems to get set in the join model.
I currently have it set up like this:
class Project < ActiveRecord::Base
has_many :collaborations
has_many :users, :through => :collaborations
end
class User < ActiveRecord::Base
has_many :collaborations
has_many :projects, :through => :collaborations
end
class Collaboration < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
Then in the console I'm typing in:
me = User.find(1)
p = Project.new(:name => "somename")
p.users << me
p.save
And what happens is that the Collaboration model gets the project_id set but not the user_id.
And the opposite happens (the user_id gets set but not the project_id) when I do something like this:
me.projects << p
Basically, it creates a "Collaboration" but only one side of the relationship gets saved.
I have been stuck at this for hours now and I feel like it's something minor - maybe something left over from when I was doing the one-to-many relationship between these two models.
---Edit---
This is what the schema looks like:
create_table "collaborations", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "shared", :default => false
t.integer "position", :default => 0
t.string "permalink"
t.text "description"
end
create_table "users", :force => true do |t|
t.string "username"
t.string "email"
t.string "crypted_password"
t.string "password_salt"
t.string "persistence_token"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.string "first_name"
t.string "last_name"
t.string "perishable_token", :default => "", :null => false
t.integer "avatar", :default => 0
end
Any help would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您正在尝试以迂回的方式建立
has_and_belongs_to_many
关系。您的协作
模型看起来与加入模型完全相同。我建议采用适当的
has_and_belongs_to_many
关系。基本上,您将成为:然后您需要为连接表创建迁移:
然后您应该能够执行
User.find(123).projects
和Project .find(321).users
,以及您尝试执行的<<
操作。有关has_and_belongs_to_many
工作原理以及可在关联上使用的各种方法的进一步说明,请参阅 Rails 文档。It sounds like you're trying to build a
has_and_belongs_to_many
relationship in a roundabout way. YourCollaboration
model looks exactly like a join model.I'd suggest going with a proper
has_and_belongs_to_many
relationship. Basically, what you have will become:And then you'll need to create a migration for the join table:
You should then be able to do
User.find(123).projects
andProject.find(321).users
, as well as the<<
you're trying to do. For further explanation of howhas_and_belongs_to_many
works, as well as the various methods you can use on the associations, please see the Rails docs.