User and Account models

This has probably been asked a few times before but I can’t find a
definite answer to my angle: The app has a User model which only keeps
the account-esque information about the user, e.g. email and password,
and nothing else. The Profile model is used to hold the data we use on a
regular basis, like first name, last name, etc. I’ve head people saying
that it’s good practice to keep these two types of data separated.

Because the regularly accessed user data is stored in the Profile model,
we’re often calling @user.profile.some_attribute, and this is making
things a little messy - I’d much rather be calling @user.some_attribute
for data which we seem to be accessing all the time. So, would it be
wise to change things round a bit and to have an Account model for the
account data and use the User model for the data we access all the time?

I’m still new to Rails and web dev in general (haven’t deployed a single
Rails app yet), so I don’t know if this is a really bad idea; based on
any experiences you have with authenticating users, would this route
work?

On 09 Aug 2008, at 11:29, Neil C. wrote:

model,
Rails app yet), so I don’t know if this is a really bad idea; based on
any experiences you have with authenticating users, would this route
work?

You can expose certain attributes in a related record by doing:

class User < ActiveRecord::Base

has_one :profile, :dependent => :destroy

delegate :first_name, :to => :profile
delegate :last_name, :to => :profile

end

It will allow you to access those attributes as @user.first_name,
@user.last_name.

Best regards

Peter De Berdt

Peter De Berdt wrote:

On 09 Aug 2008, at 11:29, Neil C. wrote:

model,
Rails app yet), so I don’t know if this is a really bad idea; based on
any experiences you have with authenticating users, would this route
work?

You can expose certain attributes in a related record by doing:

class User < ActiveRecord::Base

has_one :profile, :dependent => :destroy

delegate :first_name, :to => :profile
delegate :last_name, :to => :profile

end

It will allow you to access those attributes as @user.first_name,
@user.last_name.

Best regards

Peter De Berdt

Thanks Peter. I like the sound of that. I’m actually stuck with how to
deal with the validations on the user sign-up. The user.profile is built
in an after_create on the user model (implemented by someone else at the
beginning of the project) and I still haven’t worked out how to validate
on the profile model attributes which are required at sign-up, including
both first name and last name. Would the delegate method handle this?

On 09 Aug 2008, at 13:20, Neil C. wrote:

built
in an after_create on the user model (implemented by someone else at
the
beginning of the project) and I still haven’t worked out how to
validate
on the profile model attributes which are required at sign-up,
including
both first name and last name. Would the delegate method handle this?

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001055

I’d suggest picking up a book or two on Rails, all the information you
need will be in there and it will make the workings of the framework a
lot clearer.

Best regards

Peter De Berdt