【问题标题】:Object refuses to save changes in Rails IRB对象拒绝在 Rails IRB 中保存更改
【发布时间】:2010-08-11 15:15:59
【问题描述】:

大家早上好,

我有一个正在处理的关联,但由于某种原因,当我在 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

感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails associations irb has-one


    【解决方案1】:

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

    【讨论】:

    • 这一定与验证有关,因为当我编写一个快速表单以通过浏览器添加管理器时,它就像一个冠军。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多