# Efficient, "Rails Way" method of doing a somewhat complex fi

**URL:** <https://www.ruby-forum.com/t/efficient-rails-way-method-of-doing-a-somewhat-complex-fi/109996>\
**Category:** Rails\
**Created:** [July 21, 2007, 1:15am UTC](https://www.ruby-forum.com/t/efficient-rails-way-method-of-doing-a-somewhat-complex-fi/109996 "2007-07-21T01:15:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 21, 2007, 1:15am UTC](https://www.ruby-forum.com/t/efficient-rails-way-method-of-doing-a-somewhat-complex-fi/109996/1 "2007-07-21T01:15:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 21, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/efficient-rails-way-method-of-doing-a-somewhat-complex-fi/109996/2 "2007-07-21T18:01:18Z")

</div>

On 7/20/07, lunaclaire [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.amazon.com/dp/0321445619)

blog: [Best Online Casino in Australia | Top Licensed Casinos for Gamblers](http://jroller.com/obie/)
