1

I am relatively new to Rails.

I have a User model through Devise. I am wondering if it is more efficient to have all the additional fields i need for the user, in a separate Profile model.

I am coming across similar situations where I am considering creating a new model and using a has_one association to that model however it seems like maybe it will be cleaner if I had all the attributes belonging to a user within the User model. How do you deal with such situations? What effect will it have on application performance?

Can someone elaborate on the advantages and disadvantages of creating has_one relationship, especially in terms of performance.

alik
  • 3,820
  • 9
  • 41
  • 55

1 Answers1

0

I am also relatively new to Rails as well, but this is my take...

The benefits of associations in Rails are pretty obvious I think. In this specific case I think you are fine to go either way. Here are some things to consider...

If you use a has_one relationship, you must remember that when you are referencing the profile you will end up with something similar to this:

user = User.first

puts user.profile.first_name
puts user.profile.age

Simple enough, but if you wanted something like user.first_name you would need to delegate that method to the Profile model. This is all a matter of preference.

don_solo
  • 16
  • 2
  • 1
    ActiveRecord is relatively intelligent about not pulling from associations unless it needs to and caching that data. Another way to think about it is that if you have all the additional data on your user table then that data is accessed and queried EVERY single time someone attempts to log into the system. In my experience, having all that on the user table bogs down your app much more than associations. Also, if you reference all the data, how much does 75,000 objects with double the memory matter vs. 150,000 objects with 1/2 the memory. – rmw Aug 10 '11 at 01:46