Write join table data - has_many :through
That should be a simple question but i can't find a good solution online.
I have three tables/models. User, Alliance and Alliance_Membership. The latter is a join table descri开发者_开发知识库bing the :Alliance has_many :Users through :Alliance_Membership relationship.
Everything works ok, but Alliance_Membership now has an extra field called 'rank'. The question is, how do i set that when creating my new object ? Currently, i do something like :
@alliance.users << current_user
This is really convenient since it populates my Alliance_Membership table automatically. But, how can i set the Alliance_Membership.rank field as well ?
You'll need to create the membership yourself to set the 'rank' attribute. Something like this:
@alliance.alliance_memberships.create!(
:user => current_user,
:rank => 'whatever')
精彩评论