Extend user model with own properties

1,249 views
Skip to first unread message

Gambo

unread,
Jan 17, 2011, 5:03:48 PM1/17/11
to Devise
Hi there, I am very new to devise and ror. I want to extend my user
model which I created with devise now with several other properties.
Is it good to extend the activerecord itself or do I need to add a one
to one relation ship with user_details? I already added a new property
"t.string :page" to my users activerecord migration but its not
recognized when i run rake db:migrate.

Thanks for your help!

Regards,
Mark

Carlos Moutinho

unread,
Jan 17, 2011, 6:56:12 PM1/17/11
to plataforma...@googlegroups.com
Hi Mark,

I'm fairly new to devise but I think I can help.

I'd say it's ok to add attributes to your user model.
I would only use another model and relationship if the attributes were used elsewhere or related to another model.

About your attempt. Are you adding the new attribute to the migration generated by devise? Have you run rake db:migrate?
If you have then you should add a new migration to change the users table.
Something like this:

class AddFieldsToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :name, :string
  end

  def self.down
    remove_column :users, :name
  end
end

Hope this helps.
Cheers,
Carlos Moutinho

Gambo

unread,
Jan 18, 2011, 1:32:22 PM1/18/11
to Devise
Hi Carlos,

thanks for your help. I now extended the migration file generated by
devise and the column has been added to db. I had to delete the table
in db first before running rake db:migrate again.

Now I added the additional property to the view which I extracted with
devise beforehand. If I register the new property is not recognized as
the db value is null. Do I need to amend the controller from devise to
store also the new property beside devise standard properties?

Cheers,
Mark

On 18 Jan., 00:56, Carlos Moutinho <carlosmouti...@gmail.com> wrote:
> Hi Mark,
>
> I'm fairly new to devise but I think I can help.
>
> I'd say it's ok to add attributes to your user model.
> I would only use another model and relationship if the attributes were used
> elsewhere or related to another model.
>
> About your attempt. Are you adding the new attribute to the migration
> generated by devise? Have you run rake db:migrate?
> If you have then you should add a new migration to change the users table.
> Something like this:
>
> class AddFieldsToUsers < ActiveRecord::Migration
>   def self.up
>     add_column :users, :name, :string
>   end
>
>   def self.down
>     remove_column :users, :name
>   end
> end
>
> Hope this helps.
> Cheers,
> Carlos Moutinho
>

Andrés Mejía

unread,
Jan 18, 2011, 5:23:27 PM1/18/11
to plataforma...@googlegroups.com
Nothing needs to be done on the controller, but you need to add your new attribute to the attr_accessible list there is in the User model.

Gambo

unread,
Jan 18, 2011, 5:44:06 PM1/18/11
to Devise
Andrés,

Thanks a lot for your help, now its working!!

On 18 Jan., 23:23, Andrés Mejía <and...@gmail.com> wrote:
> Nothing needs to be done on the controller, but you need to add your new
> attribute to the attr_accessible list there is in the User model.
>
Reply all
Reply to author
Forward
0 new messages