Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 215077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:26:10+00:00 2026-05-11T18:26:10+00:00

Expanding on recent_posts_on_self below, I want to add an all_recent_posts_on_self method but I’m not

  • 0

Expanding on recent_posts_on_self below, I want to add an all_recent_posts_on_self method but I’m not sure if it’s possible using the syntax self.posts.find. On the other hand, all_recent_posts_on_class seems straightforward.

class User < ActiveRecord::Base
  has_many :posts, :class_name => "Post" , :foreign_key => "author_id"
  has_many :comments, :class_name => "Comment", :foreign_key => "author_id"

  def recent_posts_on_class
    Post.find(  :all, :conditions => ['author_id = ?', self.id],
                :order => 'created_at asc', :limit => 5)
  end

  def recent_posts_on_self
    self.posts.find(:all, :order => 'created_at ASC', :limit => 5)
  end
end

In the example above, I have two ways of finding the recent blog posts that are associated with a user. I can call Post.find and pass it the author_id or I can call self.posts.find and I don’t need to pass an author id. I assume this is because in the latter case, self.posts has already been limited based on the primary key of the user object and the has_many :posts associated with this user. This is an advantage in this case because I don’t need to go to the trouble of passing author_id as an argument. But if I did not need to limit the query by author, would it be possible to create an all_recent_posts_on_self to do this?

What I’m talking about is an equivalent of this method (which omits the :conditions):

  def all_recent_posts_on_class
    Post.find(:all, :order => 'created_at asc', :limit => 5)
  end

But using self.posts.find instead of Post.find:

  def all_recent_posts_on_self
    self.posts.find(...)
  end

Also:

Even if it’s possible to use self.posts.find to do this, is it “better” to use Post.find?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:26:10+00:00Added an answer on May 11, 2026 at 6:26 pm

    This isn’t exactly what you asked, but I consider this helpful to know and following common patterns helps avoid complicated or confusing implementations.

    The “Rails way” to do this is to use named scopes:

    class Post < ActiveRecord::Base
      belongs_to :user
      named_scope :recent, :order => 'created_at desc', :limit => 5
    end
    
    class User < ActiveRecord::Base
      has_many :posts
    end
    

    It doesn’t get much more declarative and easy to read than this:

    user.posts.recent # 5 most recent posts by the user
    Post.recent # 5 most recent posts globally
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Expanding on my previous question here , I want to know if is possible
My app is expanding, but the reqs dictate that there should be only one
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
I've built a very basic grid of divs by using float: However when expanding
i use jquery elastic plugin for expanding a textbox. it works great but i
I want animation like expanding of the photos when I click folder of photos
I'm trying to display some data points using google graphs, but unfortunately there is
Expanding on this question , it looks like I did not provide enough detail.
When expanding an item at the bottom of a QTreeView, the view does not
I've got an expanding section on a web page, and I'm using jQUery to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.