Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authlogic doesn't work with my Rails 3.2 app

I'm running Rails 3.2 and the latest version of Authlogic. When I run my app locally on my Mac, it works fine. When I try to run it on my production server (Ubuntu with Passenger/Apache), I get this:

You must establish a database connection before using acts_as_authentic

I'm not sure how to troubleshoot the problem. I posted this related question earlier today before I realized that the problem was broader than I thought.

like image 402
Jason Swett Avatar asked Mar 01 '12 15:03

Jason Swett


2 Answers

I figured out the problem. Look at this snippet from Authlogic's lib/authlogic/acts_as_authentic/base.rb:

    private
      def db_setup?
        begin
          column_names
          true
        rescue Exception
          false
        end
      end

If column_names throws an error, db_setup? will return false. Look at this other function, also from base.rb:

    def acts_as_authentic(unsupported_options = nil, &block)
      # Stop all configuration if the DB is not set up
      raise StandardError.new("You must establish a database connection before using acts_as_authentic") if !db_setup?

      raise ArgumentError.new("You are using the old v1.X.X configuration method for Authlogic. Instead of " +
        "passing a hash of configuration options to acts_as_authentic, pass a block: acts_as_authentic { |c| c.my_option = my_value }") if !unsupported_options.nil?

      yield self if block_given?
      acts_as_authentic_modules.each { |mod| include mod }
    end

If db_setup? returns false, Authlogic will throw an exception, but not the same exception thrown by column_names.

My problem was that column_names was throwing this exception:

/Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1106:in `async_exec': PG::Error: ERROR:  relation "users" does not exist (ActiveRecord::StatementInvalid)
LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"users"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

And the reason for THAT exception is that my user table is called user, not users, but Rails was not picking up on my pluralize_table_names setting properly. Once I fixed my pluralize_table_names problem (apparently the way this setting works has been changed in Rails 3.1), my Authlogic problem went away.

So if you're having this problem, you might want to try this:

  • Clone the Authlogic repo to somewhere on your dev machine
  • Change your Gemfile to use the local version of Authlogic ('authlogic', :path => '/path/to/authlogic')
  • Add a column_names call to db_setup? outside the begin/rescue/end clause
  • See if you get a different, potentially more accurate and informative, error, like I did
like image 65
Jason Swett Avatar answered Oct 02 '22 20:10

Jason Swett


I've fixed this on my fork. Until Ben has time to merge the fix you can work around this using the fixed branch in your Gemfile;

gem 'authlogic', :git => '[email protected]:james2m/authlogic.git', :branch => 'fix-migrations'
like image 26
james2m Avatar answered Oct 02 '22 22:10

james2m