大家早上好,

我有一个正在研究的关联,出于某种原因,当我在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 and affiliate.firt。我可以很好地设置用户的takaned_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

感谢您的任何帮助。

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top