Hi there, I have a favorites table in my DB that has a user_id and restaurant_id columns. In my restaurant controller I have a user ID and need to get all the restaurants that the user has marked as favorites. I would like to do something similiar to: @results = Favorite.find_all_by_user_id(current_user.id) This however returns a list of "favorites" not "restaurants". What is the best way to get the list of restaurants? Thanks, steve -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-15 22:47 UTC
Re: AR association question
@results = User.find(current_user.id).restaurants That will work if you have the habtm relationships set up correctly On Apr 15, 3:04 pm, Steve Glaz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > > I have a favorites table in my DB that has a user_id and restaurant_id > columns. > > In my restaurant controller I have a user ID and need to get all the > restaurants that the user has marked as favorites. I would like to do > something similiar to: > > @results = Favorite.find_all_by_user_id(current_user.id) > > This however returns a list of "favorites" not "restaurants". What is > the best way to get the list of restaurants? > > Thanks, > steve > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-15 22:57 UTC
Re: AR association question
Or if you don''t:
@results
Restaurant.find_all_by_id(Favorite.find_all_by_user_id(current_user.id).map
{|x| x.restaurant_id})
On Apr 15, 3:47 pm, "ESPN...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<ESPN3.DL-
Community...-xX8xgfAcNKEAvxtiuMwx3w@public.gmane.org>
wrote:> @results = User.find(current_user.id).restaurants
>
> That will work if you have the habtm relationships set up correctly
>
> On Apr 15, 3:04 pm, Steve Glaz
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> wrote:
>
> > Hi there,
>
> > I have a favorites table in my DB that has a user_id and restaurant_id
> > columns.
>
> > In my restaurant controller I have a user ID and need to get all the
> > restaurants that the user has marked as favorites. I would like to do
> > something similiar to:
>
> > @results = Favorite.find_all_by_user_id(current_user.id)
>
> > This however returns a list of "favorites" not
"restaurants". What is
> > the best way to get the list of restaurants?
>
> > Thanks,
> > steve
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks for the help. The first suggestion didn''t work - the second one did however. I have the habtm relationships set up as follows: class Favorite < ActiveRecord::Base belongs_to :restaurant belongs_to :user class User < ActiveRecord::Base has_many :favorites, :dependent => :destroy class Restaurant < ActiveRecord::Base has_many :favorites , :dependent => :destroy Any ideas why the first solution doesn''t work?(it says there is not such method "Restaurants") Thanks, steve ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> @results = User.find(current_user.id).restaurants > > That will work if you have the habtm relationships set up correctly > > > On Apr 15, 3:04�pm, Steve Glaz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 16 Apr 2008, at 15:16, Steve Glaz wrote:> > Thanks for the help. The first suggestion didn''t work - the second one > did however. I have the habtm relationships set up as follows: >You don''t have any hatbm relation ships. Add has_many :restaurants, :through => :favourites to User and you will be able to do things like User.find(...).restaurants. Fred> class Favorite < ActiveRecord::Base > belongs_to :restaurant > belongs_to :user > > class User < ActiveRecord::Base > has_many :favorites, :dependent => :destroy > > > class Restaurant < ActiveRecord::Base > has_many :favorites , :dependent => :destroy > > Any ideas why the first solution doesn''t work?(it says there is not > such > method "Restaurants") > > Thanks, > steve > > > > ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> @results = User.find(current_user.id).restaurants >> >> That will work if you have the habtm relationships set up correctly >> >> >> On Apr 15, 3:04�pm, Steve Glaz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---