Ruby on Rails Thursday, November 25, 2010

Actually, decl_auth guys told me that the problem is not with decl_auth. I think the problem could be with nested attributes. Anyone has a clue of what is this problem?

2010/11/25 Gustavo de Sá Carvalho Honorato <gustavohonorato@gmail.com>
Hi,

I'm experiencing problems with decl_auth when using a database with users table and columns names different from default. When I try to register an new user using the register form, I got the following error:

   ActiveRecord::RecordNotFound in UsersController#create

Couldn't find Role without an ID

RAILS_ROOT: /home/gustavo/CAS/cas-web

Application Trace | Framework Trace | Full Trace
/home/gustavo/.rvm/gems/jruby-1.5.2/gems/activerecord-2.3.8/lib/active_record/base.rb:1597:in `find_from_ids' /home/gustavo/.rvm/gems/jruby-1.5.2/gems/activerecord-2.3.8/lib/active_record/base.rb:619:in `find' /home/gustavo/CAS/cas-web/app/controllers/users_controller.rb:41:in `create' 

My user.rb file:

class User < ActiveRecord::Base   #==============================   #   Petro DB Convention Mapping   #==============================   TABLE_NAME = 'USUARIO'   set_table_name TABLE_NAME    FIRST_NAME_COLUMN = 'USUA_NM_NOME'   LAST_NAME_COLUMN = 'USUA_NM_SOBRENOME'   USERNAME_COLUMN = 'USUA_NM_USUARIO'   CRYPT_PASSWORD_COLUMN = 'USUA_VL_CRYPT_PASSWORD'    PASSWORD_SALT_COLUMN = 'USUA_VL_PASSWORD_SALT'   PERSISTENCE_TOKEN_COLUMN = 'USUA_VL_TOKEN'   LAST_LOGIN_AT_COLUMN = 'USUA_DT_ULTIMO_LOGIN'   LAST_LOGIN_IP_COLUMN = 'USUA_VL_IP_ULTIMO_LOGIN'   EMAIL_COLUMN = 'USUA_VL_EMAIL'   CURRENT_LOGIN_AT_COLUMN = 'USUA_DT_ATUAL_LOGIN'   CURRENT_LOGIN_IP_COLUMN = 'USUA_VL_IP_ATUAL_LOGIN'   DEPARTMENT_ID_COLUMN = 'USUA_CD_DPTO'    CREATED_AT_COLUMN = 'EVT_DT_CRIACAO'   UPDATED_AT_COLUMN = 'EVT_DT_ATUALIZACAO'    alias_attribute :first_name, FIRST_NAME_COLUMN   alias_attribute :last_name, LAST_NAME_COLUMN   alias_attribute :username, USERNAME_COLUMN   alias_attribute :crypted_password, CRYPT_PASSWORD_COLUMN    alias_attribute :password_salt, PASSWORD_SALT_COLUMN   alias_attribute :persistence_token, PERSISTENCE_TOKEN_COLUMN   alias_attribute :last_login_at, LAST_LOGIN_AT_COLUMN   alias_attribute :last_login_ip, LAST_LOGIN_IP_COLUMN   alias_attribute :email, EMAIL_COLUMN   alias_attribute :current_login_at, CURRENT_LOGIN_AT_COLUMN   alias_attribute :current_login_ip, CURRENT_LOGIN_IP_COLUMN   alias_attribute :department_id, DEPARTMENT_ID_COLUMN      alias_attribute :created_at, CREATED_AT_COLUMN   alias_attribute :updated_at, UPDATED_AT_COLUMN     acts_as_authentic do |config|     config.login_field USERNAME_COLUMN     config.crypted_password_field CRYPT_PASSWORD_COLUMN   end      acts_as_tagger    #==============================   #   Associations   #==============================    has_many :events   has_many :assignments, :dependent => :destroy   has_many :roles, :through => :assignments   has_many :event_approval_requests    belongs_to :department, :foreign_key => DEPARTMENT_ID_COLUMN    #==============================   #   Validations   #==============================    accepts_nested_attributes_for :assignments    validates_associated :roles, :assignments    validates_presence_of :first_name, :last_name, :email, :username    validates_presence_of :department_id, :unless => :admin?    def admin?     roles.first.title == Role::ADMIN_TITLE   end    def to_param     "#{id}-#{username}"   end    def role_symbols     roles.map do |role|       role.title.underscore.to_sym     end   end end 

Register Form:

<% simple_form_for @user do |f| %>
    <%= f.error_messages %>

    <%= f.input :username %>
    <%= f.input :first_name %>
    <%= f.input :last_name %>
    <%= f.input :email %>
    <%= f.association :roles, :as => :radio %>
    <%= f.association :department %>

    <%= f.button :submit %>
<% end  %>

Migration snippet:

    change_table :users do |t|
      t.rename :first_name, User::FIRST_NAME_COLUMN
      t.rename :last_name, User::LAST_NAME_COLUMN
      t.rename :username, User::USERNAME_COLUMN
      t.rename :crypted_password, User::CRYPT_PASSWORD_COLUMN

      t.rename :password_salt, User::PASSWORD_SALT_COLUMN
      t.rename :persistence_token, User::PERSISTENCE_TOKEN_COLUMN
      t.rename :last_login_at, User::LAST_LOGIN_AT_COLUMN
      t.rename :last_login_ip, User::LAST_LOGIN_IP_COLUMN
      t.rename :email, User::EMAIL_COLUMN
      t.rename :current_login_at, User::CURRENT_LOGIN_AT_COLUMN
      t.rename :current_login_ip, User::CURRENT_LOGIN_IP_COLUMN
      t.rename :department_id, User::DEPARTMENT_ID_COLUMN

      t.rename :created_at, User::CREATED_AT_COLUMN
      t.rename :updated_at, User::UPDATED_AT_COLUMN
    end

    rename_table :users, User::TABLE_NAME

Roles table:

    create_table :roles do |t|
      t.string :title

      t.timestamps
    end

Assignment table:

    create_table :assignments do |t|
      t.integer :user_id
      t.integer :role_id
      t.timestamps
    end

Thanks in advance,
Gustavo

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment