Object拒绝保存Rails IRB中的更改

时间:2010-08-11 15:15:59

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

早上好,

我有一个我正在处理的关联,出于某种原因,当我在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)。它返回就像一切都好,但是当我去保存时它只返回“假”。如果我激活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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果验证失败或模型上的任何before_*回调返回false,

ActiveRecord::Base#save将返回false。