I’ve been writing Rails for a few years, and the difference between attr_accessible :person and attr_accessible :person_id has really bugged me.
attr_accessible :person makes it irritating to deal form data, particularly with select helpers, which require access to f.select :person_id...
attr_accessible :person_id: makes it verboser to set AR finder results, like User.person_id = Person.create!(name: "Hugh").id
I don’t mind either too much, but using both seems like it should be unnecessary. I also haven’t ever really seen a preference in other people’s code. Is there a convention here I should follow?
As far as I know It’s best practice to use
attr_accessible :person_id. The model object will convert to id automatically, so you should be able to write soething like:User.first.person_id = Person.create!(name: "Hugh")or even shorter:User.first.person.create!(name: "Hugh")