对象拒绝保存 Rails IRB 中的更改

发布于 2024-09-14 06:03:26 字数 1216 浏览 5 评论 0原文

大家早上好,

我正在处理一个关联,由于某种原因,当我在 IRB 中调试它时,我无法让它保存对相关对象的更改。我想知道是否有人可以指出我的问题。

这是关联:

class User < ActiveRecord::Base
  has_and_belongs_to_many :affiliates
  has_one :managed_affiliate, :class_name => "Affiliate", :foreign_key => "manager_id"
end

class Affiliate < ActiveRecord::Base
  has_and_belongs_to_many :users
  belongs_to :manager, :class_name => "User"
  #The affiliates table has a foreign key field of manager_id
end

当我启动 IRB 时,我可以获取 User.first 和 Affiliate.first。我可以很好地设置用户的 Managed_affiliate 。然而,当我保存时,这根本没有反映在附属机构中 - 它没有经理。同样,我可以很好地设置联营公司的经理(Affiliate.first.manager = User.first)。它返回时一切正常,但当我去保存时它只是返回“false”。如果我激活 IRB 日志记录功能,则输出如下:

SQL (0.1ms)   BEGIN
SQL (0.2ms)   ROLLBACK

是否有任何明显的原因导致此关联无法正确保存?

另外,这是附属表的架构:

create_table "affiliates", :force => true do |t|
    t.string   "name"
    t.string   "website"
    t.integer  "market_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "logo_file_name"
    t.string   "logo_content_type"
    t.integer  "logo_file_size"
    t.boolean  "track_subs"
    t.integer  "manager_id"
  end

感谢您的帮助。

Good morning all,

I've got an association that I'm working on, and for some reason, I can't get it to save changes to the object in question when I'm debugging it in IRB. I was wondering if anyone could point out my problem.

Here is the association:

class User < ActiveRecord::Base
  has_and_belongs_to_many :affiliates
  has_one :managed_affiliate, :class_name => "Affiliate", :foreign_key => "manager_id"
end

class Affiliate < ActiveRecord::Base
  has_and_belongs_to_many :users
  belongs_to :manager, :class_name => "User"
  #The affiliates table has a foreign key field of manager_id
end

When I fire up the IRB, I can grab a User.first and Affiliate.first. I can set the user's managed_affiliate just fine. However, when I save, that is not reflected at all in the affiliate - it has no manager. Likewise, I can set the affiliate's manager just fine (Affiliate.first.manager = User.first). It returns like everything is ok, but when I go to save it simply returns "false." If I activate the IRB logging function, this is the output:

SQL (0.1ms)   BEGIN
SQL (0.2ms)   ROLLBACK

Is there any obvious reason why this association won't save properly?

Also, here is the schema for the affiliates table:

create_table "affiliates", :force => true do |t|
    t.string   "name"
    t.string   "website"
    t.integer  "market_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "logo_file_name"
    t.string   "logo_content_type"
    t.integer  "logo_file_size"
    t.boolean  "track_subs"
    t.integer  "manager_id"
  end

Thank you for any help.

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

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

发布评论

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

评论(1

橙幽之幻 2024-09-21 06:03:26

ActiveRecord::Base#save 将如果验证失败或模型上的任何 before_* 回调返回 false,则返回 false。

ActiveRecord::Base#save will return false if a validation fails or if any before_* callbacks on the model returns false.

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