I’m setting up a Rails 3.1 application in which users can change the “theme” of their page to one of several premade themes. I’m having trouble figuring out which ActiveRecord association properly models the relationship between a User and a Theme.
I want to write something like:
class User < ActiveRecord::Base
has_one :theme
class Theme < ActiveRecord::Base
belongs_to_many :users
But “belonds_to_many” isn’t a real association, and has_one puts the foreign key in the Theme table. (according to Association Basics)
What is the canonical way to for an ActiveRecord “User” to have a single “Theme”, where a “Theme” can be used by many “Users”?
The user should belong to the theme in this case.
In this case, the user table would have a field called theme_id.
The belongs_to feels weird in this case, when thinking of it from an English language perspective, since a user shouldn’t really belong to a theme. However, the general rule of thumb is that the model containing the foreign key belongs to the model which doesn’t.