lunaclaire
2007-Jul-20 23:14 UTC
efficient, "Rails Way" method of doing a somewhat complex find
I''m still a newb w/r/t rails and ruby, so this may be obvious, but I havent found an answer in my searching yet... I have a visibility attribute on a Story model that says some of the objects can only be seen by "friends" and some just by "family". I also have a Contacts model where each object has an attribute to mark them as a Friend or Family for a given User. My User model hasMany Stories and hasMany Contacts. Maybe you see where this is going... When a user is logged in, I want to show them only the Stories that should be visible to them as determined by one of the following two logical matches: a) the Story is only visible to Friends and the user is in the Story owner''s Friends list b) the Story is only visible to Family and the user is in the Story owner''s Family list What I''m looking for is the best way to do this by doing it "the Rails Way" and being as efficient as possible. I was starting to approach it like this: 1) get the list of users for which the current user is in their Friends list 2) get the list of users for which the current user is in their Family list 3) find and show Stories where visibility = Story::VISIBLITY_FRIENDS and current_user is in list 1 OR where visibility Story::VISIBLITY_FAMILY and current_user is in list 2 ...but this seems clumsy and maybe a performance hog... also I''m not sure yet how to do a SQL IN clause in Rails. Anyone have suggestions for a better approach? And could someone show me how to do that IN clause in a find method (I''ve searched and haven''t found it yet)? Guess I could do a find_by_sql call... Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Obie Fernandez
2007-Jul-21 16:00 UTC
Re: efficient, "Rails Way" method of doing a somewhat complex find
On 7/20/07, lunaclaire <szagerman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m still a newb w/r/t rails and ruby, so this may be obvious, but I > havent found an answer in my searching yet... >Here is the most concise solution I could come up with based on your description and off the "top of my head". I didn''t test this code, so I don''t guarantee it 100%. Whenever someone is looking at a page of another user''s stories, you have the two IDs with which to find a Contact instance. From that contact instance you can simply call .stories... Have fun! (And as you see an IN clause wasn''t even necessary.) # == Schema Information # Table name: users # id :integer(11) not null, primary key # name :string(255) class User < ActiveRecord::Base has_many :stories end # == Schema Information # Table name: contacts # id :integer(11) not null, primary key # from_id :integer(11) # to_id :integer(11) # family :boolean # friend :boolean class Contact < ActiveRecord::Base belongs_to :from_user, :class_name => "User", :foreign_key => "from_id" belongs_to :to_user, :class_name => "User", :foreign_key => "to_id" def stories conditions = {} conditions[:family_only] = false unless to_user.family? conditions[:friend_only] = false unless to_user.friend? from_user.stories.find(:all, :conditions => conditions) end end # == Schema Information # Table name: stories # id :integer(11) not null, primary key # user_id :integer(11) # family_only :boolean # friend_only :boolean class Story < ActiveRecord::Base belongs_to :user end Cheers and good luck, Obie -- Pre-order my book The Rails Way http://www.amazon.com/dp/0321445619 blog: http://jroller.com/obie/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---