开发者

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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜