Changing entries in my migration results in authlogic error
I add multiple columns to one of my database tables and then populate these 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
throws the following error when using Authlogic:
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 am not using a controller method to change every row in the table. I also don't create new objects.
Error if the contact table is empty.
I did google it and it looks like this error might appear when running your controller tests and is fixed by adding to them before_filter :activate_authlogic
, but it 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
Release Notes : Rails 2.3.5, Authlogic 2.1.3
rake db: migrate --trace output can be found online here: http://pastie.org/944446
Browser information:
I have ActivityObserver
one that observes my model Contact
and creates Activity
using a callback after_update
.
In my model, Activity
I hackishly associate @current user
with an activity created using a callback before_save
.
Here's the relevant code snippets:
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 id
self.user = UserSession.find.record
end
end
This is definitely where Authlogic is involved. KandadaBoggu is a winrar - thanks for your understanding!
In terms of fixes, I think there are basically none. If I want to create an Activity when my contact is updated by migration, there is by definition no @current_user
for communication. I'll think about how to get around this, but KandadaBoggu definitely answered my question.
a source to share
It is similar to how the Authlogic session object is created when the Contact model is updated. Do you have observers OR filters before / after for the model Contact
?
From the authlogic documentation for Authlogic::Session::Base.activated?
# Returns true if a controller has been set and can be used properly.
# This MUST be set before anything can be done. Similar to how ActiveRecord
# won't allow you to do anything without establishing a DB connection. In your
# framework environment this is done for you, but if you are using Authlogic
# outside of your framework, you need to assign a controller object to Authlogic
# via Authlogic::Session::Base.controller = obj. See the controller= method for
# more information.
One way to work around the problem is by setting controller
before modifying the model instance Contact
, that is:
def self.up
add_column :contacts, :business_id, :integer
add_column :contacts, :business_type, :string
Authlogic::Session::Base.controller =
Authlogic::ControllerAdapters::RailsAdapter.new(self)
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
Note: I have not tested this code.
a source to share
I'm not sure if this will work, but it's worth trying:
replace this line:
contact.update_attributes(:business_id => contact.client_id, :business_type => 'Client')
from:
contact.update_attribute(:business_id, contact.client_id)
contact.update_attribute(:business_type, 'Client')
I cannot reproduce your problem, so I only assume:
For some reason update_attributes is throwing this exception. I assume this during the verification phase. Therefore, we skip checking with update_attribute.
If that still doesn't work, then the exception is thrown further down the stack. In the worst case, you can write some SQL to update your models.
a source to share
note: I am assuming you have a model called User in your application. If not, depending on what act_as_authentic is using, I mean.
Short version: Add the following to your transfer:
User.controller = true
or
UserSession.controller = true
If fixed, the problem is that your migration is trying to load your user model, which requires a customized controller.
View http://github.com/binarylogic/authlogic/blob/v2.1.3/lib/authlogic/session/activation.rb source is probably the place you want to run if what I described doesn't work ...
EDIT:
Add to the start of your migration:
module Authlogic::ActsAsAuthentic::Base::Config
def acts_as_authentic(unsupported_options = nil, &block)
end
end
This should disable the act_as_authentic method so that your model doesn't pull all the extra stuff.
a source to share