I have a habtm relationship between User and Activity
when I create a new Activity why do I have to manually set the foreign
key
user_id in the activity table???
example
def create @activity = Activity.new(params[:activity]) @activity.user_id = self.current_user.id #why is this
necessary??? but I guess rails can’t magically know what I mean by
user???
can it?
if @activity.save @activity.users << User.find_by_id(self.current_user.id) #habtm
end
I’m a little confused by your example. Why does Activity have a
user_id on it if you have a HABTM relationship? There should be a
join table named “activities_users” that has only two columns,
“activity_id” and “user_id”, and you shouldn’t need a user_id column
on Activity.
You’re forgetting that @activity is just an AR object, not a database
entry. In order to propagate the changes in the database you should
call the save method:
You’re forgetting that @activity is just an AR object, not a database
entry. In order to propagate the changes in the database you should
call the save method: