Authlogic with nested attributes and polymorphic associations

Posted by ferparra on Stack Overflow See other posts from Stack Overflow or by ferparra
Published on 2010-04-06T01:07:29Z Indexed on 2010/04/06 1:13 UTC
Read the original article Hit count: 565

Hi all!

I'm having trouble with the following code:

User < AR
  acts_as_authentic

  belongs_to :owner, :polymorphic => true
end

Worker < AR
  has_one :user, :as => :owner
  accepts_nested_attributes_for :user
end

Employer < AR
  has_one :user, :as => :owner
  accepts_nested_attributes_for :user
end

I'd like to create registration forms based on user types, and to include authentication fields such as username and password. I currently do this:

UserRegistrationController < AC
  #i.e. a new Employer
  def new
    @employer = Employer.new
    @employer.build_user
  end
...
end

I then include User fields with fields_for. All views render fine, but here's the catch: I cannot build a User, it tells me :password is a wrong method, so I guess the authentication logic was bypassed. What should I do? Am I doing it wrong altogether? Should I drop polymorphic associations in favor of Single Table Inheritance? Whatever I do, I have to make sure it plays nicely with Authlogic.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about authlogic