Rails 3.0.5 own_to 关联未更新声明类中的主键

发布于 2024-10-21 11:48:51 字数 1138 浏览 2 评论 0原文

我正在尝试做一个基本的“belongs_to/has_many”关联,但遇到了问题。似乎声明类的外键列没有被更新。这是我的模型:


#
# Table name: clients
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Client < ActiveRecord::Base
  has_many :units
  ...
  attr_accessible :name
end

#
# Table name: units
#
#  id         :integer         not null, primary key
#  client_id  :integer
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Unit < ActiveRecord::Base
  belongs_to :client
  ...
  attr_accessible :name
end

当我打开 Rails 控制台时,我执行以下操作:

#This works as it should
c1 = Client.create(:name => 'Urban Coding')
u1 = c1.units.create(:name => 'Birmingham Branch')

上面给出了正确的结果。我有一个客户和一个单位。该单元已正确填充 client_id 外键字段。

#This does not work.
c1 = Client.create(:name => 'Urban Coding')
u1 = Unit.create(:name => 'Birmingham Branch')

u1.client = c1

我感觉上面的效果应该是一样的。然而事实并非如此。我有一个单位和一个客户端,但单位 client_id 列未填充。不确定我在这里做错了什么。感谢帮助。如果您需要更多信息,请告诉我。

I am trying to do a basic belongs_to/has_many association but running into a problem. It seems that the declaring class's foreign key column is not being updated. Here are my models:


#
# Table name: clients
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Client < ActiveRecord::Base
  has_many :units
  ...
  attr_accessible :name
end

#
# Table name: units
#
#  id         :integer         not null, primary key
#  client_id  :integer
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Unit < ActiveRecord::Base
  belongs_to :client
  ...
  attr_accessible :name
end

When I open rails console I do the following:

#This works as it should
c1 = Client.create(:name => 'Urban Coding')
u1 = c1.units.create(:name => 'Birmingham Branch')

The above gives me the correct results. I have a client and a unit. The unit has the client_id foreign key field populated correctly.

#This does not work.
c1 = Client.create(:name => 'Urban Coding')
u1 = Unit.create(:name => 'Birmingham Branch')

u1.client = c1

I feel the above should have the same effect. However this is not the case. I have a unit and a client but the units client_id column is not populated. Not sure exactly what I am doing wrong here. Help is appreciated. Let me know if you need more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

抱猫软卧 2024-10-28 11:48:51

您只是没有保存 u1,因此不会更改数据库。

如果您希望在单个操作中分配并保存它,请使用 update_attribute

u1.update_attribute(:client, c1)

You're simply not saving u1, hence no change to the database.

If you want it assigned and saved in a single operation, use update_attribute

u1.update_attribute(:client, c1)
三生一梦 2024-10-28 11:48:51

是的,我想如果你保存它,ID 就会被设置。

第一个语法要好得多。如果您不想立即执行保存操作(create 会为您执行此操作),则可以使用 build:

c1 = Client.create(:name => 'Urban Coding')
u1 = c1.units.build(:name => 'Birmingham Branch')
# do stuff with u1
u1.save

Yep, I think if you save it the ID will get set.

The first syntax is much better. If you don't want to do the save action right away, which create does for you, then use build:

c1 = Client.create(:name => 'Urban Coding')
u1 = c1.units.build(:name => 'Birmingham Branch')
# do stuff with u1
u1.save
坏尐絯 2024-10-28 11:48:51

这是可行的:

c1 = Client.create(:name => 'Urban Coding')
u1 = Unit.create(:name => 'Birmingham Branch')

u1.client_id = c1.id

u1.save
c1.save

但另一种方法是更好的创建方法。

This works:

c1 = Client.create(:name => 'Urban Coding')
u1 = Unit.create(:name => 'Birmingham Branch')

u1.client_id = c1.id

u1.save
c1.save

but the other way is the better way to create it.

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