First I want to thank the community for their help and here is my question :) I have a simple add to fdavourites functionality. A user can add a post to their favouirites. Thing is I want to display the "add to favourites" button if the post is not in the user''s favs and something else if it is, so I need to check for this in the view. favourites table has post_id and user_id I tried something like <% if current_user.favourites.exists? @post, :post_id %> <p>already faved</p> <% else %> blabla not faved :) <% end %> I''m pretty sure the error is in the "@post, :post_id" part of my code but I couldn''t find anything about the .exists? syntax in google. Thanks again -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think you need .any? On May 17, 3:50 pm, Stefan Gelenchev <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> First I want to thank the community for their help and here is my > question :) > > I have a simple add to fdavourites functionality. A user can add a post > to their favouirites. Thing is I want to display the "add to favourites" > button if the post is not in the user''s favs and something else if it > is, so I need to check for this in the view. > > favourites table has post_id and user_id > > I tried something like > > <% if current_user.favourites.exists? @post, :post_id %> > <p>already faved</p> > <% else %> > blabla not faved :) > <% end %> > > I''m pretty sure the error is in the "@post, :post_id" part of my code > but I couldn''t find anything about the .exists? syntax in google. > > Thanks again > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Played with the code for hours and just got it to work, 20 minutes after posting here. ruby-forum.com brought me luck, thank you :) Here the working code: <% if current_user.favourites.exists? :post_id => @post.id %> So it checks the favs of the current_user in the fawvourites table and compares the post_id ( which is the id of the already faved post) to the current post that is displayed on the page. Hope this helps someone else too :) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Learn something new every day. If anyone else is wondering if that is slower or faster then current_user.favorites.where(:post_id => @post.id).count == 1 the SQL from the .exists? method is: SELECT 1 FROM "favorites" WHERE "favorites"."post_id" = 666 AND "favorites".user_id = 1 LIMIT 1 On May 17, 4:11 pm, Stefan Gelenchev <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Played with the code for hours and just got it to work, 20 minutes after > posting here. ruby-forum.com brought me luck, thank you :) > > Here the working code: > > <% if current_user.favourites.exists? :post_id => @post.id %> > > So it checks the favs of the current_user in the fawvourites table and > compares the post_id ( which is the id of the already faved post) to the > current post that is displayed on the page. > > Hope this helps someone else too :) > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 May 18, 2:15 pm, PsiPro <arjes...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Learn something new every day. > > If anyone else is wondering if that is slower or faster then > current_user.favorites.where(:post_id => @post.id).count == 1 the SQL > from the .exists? method is: >if there is genuinely one such favourite then i don''t think it makes much different. If there were hundreds (or thousands) then the count method needs to find them all before it can return its result whereas the sql from .exists? only needs to find one such row Fred> SELECT 1 FROM "favorites" WHERE "favorites"."post_id" = 666 AND > "favorites".user_id = 1 LIMIT 1 > > On May 17, 4:11 pm, Stefan Gelenchev <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > Played with the code for hours and just got it to work, 20 minutes after > > posting here. ruby-forum.com brought me luck, thank you :) > > > Here the working code: > > > <% if current_user.favourites.exists? :post_id => @post.id %> > > > So it checks the favs of the current_user in the fawvourites table and > > compares the post_id ( which is the id of the already faved post) to the > > current post that is displayed on the page. > > > Hope this helps someone else too :) > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.