Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 754243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:59:23+00:00 2026-05-14T14:59:23+00:00

I’m adding some columns to one of my database tables, and then populating those

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:59:24+00:00Added an answer on May 14, 2026 at 2:59 pm

    It looks like some how Authlogic session object is created when you update the Contact model. Do you have any observers OR before/after filters for Contact model?

    From 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 issue by setting the controller before modifying the Contact model instance, i.e.:

    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 haven’t tested this code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.