# Finding data in Rails

**URL:** <https://www.ruby-forum.com/t/finding-data-in-rails/174376>\
**Category:** Rails\
**Created:** [August 25, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376 "2009-08-25T22:34:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 25, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/1 "2009-08-25T22:34:52Z")

</div>

Hi All,

I have 3 model, User, Post, Comment with definition like below

```
class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  def self.find_other_user_posts
       ?
  end
end
class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  has_many :posts_commented_on, :through => :comments, :source

```

=\> :posts  
end  
class Comment\< ActiveRecord::Base  
belongs\_to :post  
belongs\_to :user  
end

User can have many post and many comment, question is I want to make  
method in Post model that returning collection of posts. The method  
will find posts and comments that doesn’t belong to current user. For  
example there are

Post: A have Comment: A1  
Post: B have Comment: B1, B2, B3, B4  
Post: C have Comment: C1, C2  
Post: D have Comment: nil  
Post: E have Comment: nil  
Post: F have Comment: F1, F2, F3

current user own Post A, E and Comment A1, B2, so calling :

@posts = Post.find\_other\_user\_posts

will return collection of C, D, F posts.

Thanks

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 25, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/2 "2009-08-25T22:58:17Z")

</div>

If you want that to be a class method on Post you’ll have to pass in  
the id of the user I think.

How about something like:

def self.find\_other\_users\_posts(this\_user)  
self.find(:all, :conditions =\> [“user\_id \<\> ?”, this\_user.id])  
end

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 26, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/3 "2009-08-26T12:25:59Z")

</div>

Make it a named\_scope instead of a function so that it can be chained  
in the future.

Class Post  
named\_scope :other\_users\_posts, lambda { |user\_object| {:conditions=\>  
[“user\_id \<\> ?”, user\_object.id]}}  
end

You can then do a Post.other\_users\_post(User.first) as an example and  
chain it just like other active record finder functions.

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 25, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/4 "2009-08-25T23:32:13Z")

</div>

My two cents would be to have a look at Searchlogic, It’s my latest  
favorite and I find a use for it in every rails project I do…

> **[GitHub - binarylogic/searchlogic: Searchlogic provides object based...](https://github.com/binarylogic/searchlogic)**
>
> Searchlogic provides object based searching, common named scopes, and other useful tools. - GitHub - binarylogic/searchlogic: Searchlogic provides object based searching, common named scopes, and o...

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 26, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/5 "2009-08-26T19:41:40Z")

</div>

@heimdull, wow I search and learn “searchlogic” and it’s very very  
logic  
and super duper easy, thanks. (sorry, mistype)

---

<div class="post-metadata">

**Author:** ![Geekyra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Geekyra](https://www.ruby-forum.com/u/Geekyra)\
**Post date:** [August 26, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/finding-data-in-rails/174376/6 "2009-08-26T19:31:43Z")

</div>

Ah, @Roy, sorry but the solution doesn’t work.  
The question is to return C, D, F because current user doesn’t own C,  
D, F, and current user doesn’t make any comment on C, D, F.  
Actually there are 2 condition I want to achieve, I want to return  
collection of post that :

1. Other than my own post OR Haven’t read it yet OR Post not created  
by me.
2. And Post that haven’t got my Comment at all because if I’m already  
make a Comment on a Post then it assume that I’m already read the post  
before.

So what I want is a Post that is very very new to me and I want to  
read it and make Comment. How to do that ?

@Roy, wow I search and learn “searchlogic” and it’s very very logic  
and super duper easy, thanks.  
@Mukund, hanks for named\_scope suggestion I search the article and  
learn it and found out IT’S VERY COOL, especially the chain part. 🙂
