Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top