Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 561
ryanprayogo Avatar asked Apr 27 '11 03:04

ryanprayogo


People also ask

What is a polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

What is dependent destroy in Rails?

Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.

What is association in Ruby on Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship.

What is self association in Rails?

Self-referential association means we create a JOIN MODEL, such as Friendship, for example, which links another model, such as User to itself, so a user can have many friends (which are other users), and a friend can be befriended by a user ( a follower and a followed).


1 Answers

current_user.score_cards << score_card 

OR

score_card.user = current_user score_card.save 
like image 99
grzuy Avatar answered Oct 14 '22 12:10

grzuy