Given two models:
class User < ActiveRecord::Base
attr_accessible :name, :user_status
end
class UserStatus < ActiveRecord::Base
attr_accessible :descr
end
The user.user_status column is intended to be a single ID which can be used to join against UserStatus for additional information, such as status icon and description.
UserStatus is a read-only reference table which will have very few rows, all unique. Let’s say it has only four rows:
{ :id => 0, :descr => 'New' },
{ :id => 1, :descr => 'Banned' },
{ :id => 2, :descr => 'Active' },
{ :id => 3, :descr => 'Unverified' }
Should I bother with ActiveRecord associations for reference tables? If so, what is the proper ActiveRecord association if I want to (or must) store the associated UserStatus ID in the user table? From what I can tell, based on the documentation, ‘User belongs_to :user_status’ is my best bet, but linguistically this is failure; because a User does not “belong to” a status, a user is in a state or has a status. Or is this just too hair-splitting of me?
belongs_todoesn’t always make linguistic sense, but in this case it’s just what you want.Which also allows the status to find all users in that state, which can be useful.
What’s more important, linguistically, is the API you settle on to get this data.
That kind of sucks. It has the relations you want, but the API is pretty verbose.
Much better.
Or even possibly this!
How far you want to massage it is up to you.