What exactly last line of code is doing?
@user = find开发者_如何学Python_user
@user_sport = UserSport.new(params[:iuser_sport])
@user.user_sports << @user_sport
What exactly last line of code is doing??
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.
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.
精彩评论