Rails 3 has_many :through 只是从关系的一侧添加 id

发布于 2024-10-12 20:34:28 字数 1817 浏览 3 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-10-19 20:34:29

听起来您正在尝试以迂回的方式建立 has_and_belongs_to_many 关系。您的协作模型看起来与加入模型完全相同。

我建议采用适当的 has_and_belongs_to_many 关系。基本上,您将成为:

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

然后您需要为连接表创建迁移:

def self.up
  create_table :projects_users, :id => false do |t|
    t.references :project, :user
  end
end

def self.down
  drop_table :projects_users
end

然后您应该能够执行 User.find(123).projectsProject .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. Your Collaboration 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:

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

And then you'll need to create a migration for the join table:

def self.up
  create_table :projects_users, :id => false do |t|
    t.references :project, :user
  end
end

def self.down
  drop_table :projects_users
end

You should then be able to do User.find(123).projects and Project.find(321).users, as well as the << you're trying to do. For further explanation of how has_and_belongs_to_many works, as well as the various methods you can use on the associations, please see the Rails docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文