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 282545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:17:52+00:00 2026-05-12T05:17:52+00:00

I have two models in a has_many relationship such that Log has_many Items. Rails

  • 0

I have two models in a has_many relationship such that Log has_many Items. Rails then nicely sets up things like: some_log.items which returns all of the associated items to some_log. If I wanted to order these items based on a different field in the Items model is there a way to do this through a similar construct, or does one have to break down into something like:

Item.find_by_log_id(:all,some_log.id => "some_col DESC")
  • 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-12T05:17:52+00:00Added an answer on May 12, 2026 at 5:17 am

    There are multiple ways to do this:

    If you want all calls to that association to be ordered that way, you can specify the ordering when you create the association, as follows:

    class Log < ActiveRecord::Base
      has_many :items, :order => "some_col DESC"
    end
    

    You could also do this with a named_scope, which would allow that ordering to be easily specified any time Item is accessed:

    class Item < ActiveRecord::Base
      named_scope :ordered, :order => "some_col DESC"
    end
    
    class Log < ActiveRecord::Base
      has_many :items
    end
    
    log.items # uses the default ordering
    log.items.ordered # uses the "some_col DESC" ordering
    

    If you always want the items to be ordered in the same way by default, you can use the (new in Rails 2.3) default_scope method, as follows:

    class Item < ActiveRecord::Base
      default_scope :order => "some_col DESC"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that has a manytomany relationship with a 'through' table in
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
Say that I have two models- Users and Accounts. Each account can have at
I have two models I want to connect with an m-to-m relationship, but I
I have a relationship in a Core Data model that feels like it wants
I have two models -- User and Entry -- that are related through a
I have two models Project and 'Task` where project has_many tasks and task belongs
I have two models: Books and Authors. Books has_many Authors Authors belongs_to Books Problem
I'm new to Rails. I have two models, Person and Day. class Person <

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.