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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:12:28+00:00 2026-05-23T07:12:28+00:00

I have three models, basically: class Vendor has_many :items end class Item has_many :sale_items

  • 0

I have three models, basically:

class Vendor
  has_many :items
end

class Item
  has_many :sale_items
  belongs_to :vendor
end

class SaleItem
  belongs_to :item
end

Essentially, each sale_item points to a specific item (but has an associated quantity and sale price which might be different from the item‘s base price, hence the separate model), and each item is made by a specific vendor.

I’d like to sort all sale_items by vendor name, but this means going through the associated item, because that’s where the association is.

My first attempt was to change SaleItem to the following:

class SaleItem
  belongs_to :item
  has_one :vendor, :through => :item
end

Which allows me to look for SaleItem.first.vendor, but doesn’t allow me to do something like:

SaleItem.joins(:vendor).all(:order => "vendors.name")

Is there an easy way to figure out these complex associations and sorting? It would be especially great if there were a plugin that could take care of these sort of things. I have a lot of different types of tables to add sorting to in this application, and I feel like this will be a big chunk of the figuring-out work.

  • 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-23T07:12:29+00:00Added an answer on May 23, 2026 at 7:12 am

    This could definitely be done with a more complex SQL query (possibly using find_by_sql), but you could also do it pretty easily in Ruby. Try something like the following:

    SaleItem.find(:all, :include => { :items => :vendors }).sort do |first,second|
      first.vendor.name <=> second.vendor.name
    end
    

    I haven’t tested it, so it might not work exactly like this, but it should give you a good idea of one possible solution.

    Edit: Found an old blog post that seems to have solved this issue. Hopefully this still works in the lastest version of ActiveRecord.

    source: http://matthewman.net/2007/01/04/eager-loading-objects-in-a-rails-has_many-through-association/

    Second Edit: Straight from the Rails documentation

    To include a deep hierarchy of associations, use a hash:

    for post in Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])

    That’ll grab not only all the comments but all their authors and gravatar pictures. You can mix and match symbols, arrays and hashes in any combination to describe the associations you want to load.

    There’s your explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models: class A < ActiveRecord::Base has_many :bs end class B <
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
Say I have a three models that look (basically) like this: class User <
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have 2 models: class Video < ActiveRecord::Base belongs_to :categories, :foreign_key => category, :class_name
Let's say I have a class structure that is defined below: Class Item(models.Model): ...

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.