Forum: Ruby on Rails habtm foreign key must be manually done

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adebae130af75724f0296ff49958d6fb?d=identicon&s=25 Dion Hewson (Guest)
on 2007-05-24 12:23
(Received via mailing list)
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

end


cheer

dion
505cf9ac8f46a71a66197e554b69630a?d=identicon&s=25 Seth Morabito (Guest)
on 2007-05-25 11:29
(Received via mailing list)
Hi Dion,

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.

This line:

     @activity.users << User.find_by_id(self.current_user.id )   #habtm

should also probably just be

     @activity.users << current_user

to save yourself some database hits.


Regards,

-Seth
78e258248fa0c682fec8c18c66532625?d=identicon&s=25 Andrei Maxim (Guest)
on 2007-05-25 14:44
(Received via mailing list)
On 5/24/07, Dion Hewson <dionhewson@gmail.com> wrote:
> 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???

You don't have to. The habtm relationship should set everything up
automatically.

>         @activity.users << User.find_by_id(self.current_user.id )   #habtm
> end
>
>  end

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:

@activity.users << User.find_by_id(self.current_user.id )
@activity.save


--
Andrei Maxim
http://andreimaxim.ro
78e258248fa0c682fec8c18c66532625?d=identicon&s=25 Andrei Maxim (Guest)
on 2007-05-25 14:47
(Received via mailing list)
On 5/25/07, Andrei Maxim <andreicmaxim@gmail.com> wrote:
...

> 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:
>
> @activity.users << User.find_by_id(self.current_user.id )
> @activity.save

Actually you should try

@activity.users << current_user
@activity.save

because you won't have to do an extra query.

--
Andrei Maxim
http://andreimaxim.ro
Adebae130af75724f0296ff49958d6fb?d=identicon&s=25 Dion Hewson (Guest)
on 2007-05-25 14:56
(Received via mailing list)
@activity.users << current_user

does the trick, cheers

still curbing my old school thinking
This topic is locked and can not be replied to.