I'm adding some columns to one of my database tables, and then populating those columns:
def self.up
add_column :contacts, :business_id, :integer
add_column :contacts, :business_type, :string
Contact.reset_column_information
Contact.all.each do |contact|
contact.update_attributes(:business_id => contact.client_id, :business_type => 'Client')
end
remove_column :contacts, :client_id
end
The line contact.update_attributes
is causing the following Authlogic error:
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
I have no idea what is going on here - I'm not using a controller method to modify each row in the table. Nor am I creating new objects.
The error doesn't occur if the contacts table is empty.
I've had a google and it seems like this error can occur when you run your controller tests, and is fixed by adding before_filter :activate_authlogic
to them, but this doesn't seem relevant in my case.
Any ideas? I'm stumped.
Here's my Contact model as requested:
class Contact < ActiveRecord::Base
belongs_to :contactable, :polymorphic => true
has_many :phone_numbers, :as => :callable
has_many :email_addresses, :as => :emailable
accepts_nested_attributes_for :phone_numbers
accepts_nested_attributes_for :email_addresses
validates_presence_of :first_name, :last_name
validates_format_of :first_name, :last_name, :with => /^[-a-zA-Z ]+$/
default_scope :order => 'first_name ASC, last_name ASC'
def full_name
"#{self.first_name} #{self.last_name}"
end
def to_s
full_name
end
end
Version info: Rails 2.3.5, Authlogic 2.1.3
rake db:migrate --trace output can be found online at pastie here: http://pastie.org/944446
Observer info:
I have an ActivityObserver
that is observing my Contact
model and creating an Activity
using the after_update
callback.
In my Activity
model I am hackishly associating @current user
with the activity being created using the before_save
callback.
Here's the relevant snippets of code:
class ActivityObserver < ActiveRecord::Observer
observe :contact
def after_update(subject)
Activity.create(:action => 'Updated', :subject => subject)
end
end
class Activity < ActiveRecord::Base
belongs_to :subject, :polymorphic => true
belongs_to :user
def before_save
# FIXME: This is a messy hack way to get the user's id
self.user = UserSession.find.record
end
end
This is definitely where Authlogic is getting involved. KandadaBoggu is the winrar - thanks a lot for your insight!!!
In terms of fixes, I think that fundamentally there aren't any. If I want to create an Activity when my Contact is updated through a migration, by definition there is no @current_user
to associate. I'll have a think about a way to get around this, but KandadaBoggu has definitely answered my question.