Rails - Associations - Automatically setting an association_id for a model which has 2 belongs_to
Posted
by adam
on Stack Overflow
See other posts from Stack Overflow
or by adam
Published on 2010-04-12T13:47:07Z
Indexed on
2010/04/12
21:42 UTC
Read the original article
Hit count: 370
ruby-on-rails
|associations
I have 3 models
class User < ...
belongs_to :language
has_many :posts
end
class Post < ...
belongs_to :user
belongs_to :language
end
class Language < ...
has_many :users
has_many :posts
end
Im going to be creating lots of posts via users and at the same time I have to also specify the language the post was written in, which is always the language associatd with the user i.e.
@user.posts.create(:text => "blah", :language_id => @user.language_id)
That's fine but the way I set the language doesn't sit well with me. The language will always be that of the users so is there a 'best-practice' way of doing this?
I know a little about callbacks and association extensions but not sure of any pitfalls.
© Stack Overflow or respective owner