Rails association - how to add the 'has_many' object to the 'owner'

ryanprayogo picture ryanprayogo · Apr 27, 2011 · Viewed 28.8k times · Source

In my app, a user has many score_cards and a score_card belongs to a user

The question is, whenever I create a new score_card, ie, ScoreCardsController.create gets called, how do I add this newly created score_card to the current_user (I'm using devise, so current_user is a valid User object).

Answer

grzuy picture grzuy · Apr 27, 2011
current_user.score_cards << score_card

OR

score_card.user = current_user
score_card.save