user659068
user659068

Reputation: 343

What exactly last line of code is doing?

@user = find_user
@user_sport = UserSport.new(params[:iuser_sport])
@user.user_sports << @user_sport

What exactly last line of code is doing??

Upvotes: 1

Views: 77

Answers (2)

McStretch
McStretch

Reputation: 20645

It is appending @user_sport to the user_sports array.

More info: <<

Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.

push is also an equivalent method if you prefer to see the word. << is common though, so it comes down to personal preference.

Upvotes: 2

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

From rails API doc

Adds one or more objects to the collection by creating associations in the join table (collection.push and collection.concat are aliases to this method).

the '<<' creates association between activeRecords object,

here User has many UserSports so @user.user_sports << @user_sport defines association between @user and @user_sport.

Upvotes: 2

Related Questions