Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Order clause gets dropped on has_one through #10016
In the following situation, I would expect the order clause of the has_one relation to carry over to the has_one through, but it has to be explicitly stated again for it to be included in the generated SQL.
class Suite has_one :tenancy, order: 'tenancies.started_at DESC' has_one :tenant, through: :tenancy, order: 'tenancies.started_at DESC' end
PS I realize I'm kind of using a hack with the has_one here.
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
Same thing happens with a has_many so just used that since I know how to get the generated sql.
gem 'activerecord', '3.2.11' require 'active_record' require "minitest/autorun" require 'minitest/pride' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') ActiveRecord::Schema.define do create_table :suites, force: true do |t| end create_table :tenancies, force: true do |t| t.integer :suite_id t.integer :tenant_id t.datetime :started_at end create_table :tenants, force: true do |t| end end class Suite < ActiveRecord::Base has_many :tenancies, order: 'tenancies.started_at DESC' has_many :tenants, through: :tenancies end class Tenancy < ActiveRecord::Base belongs_to :suite belongs_to :tenant end class Tenant < ActiveRecord::Base end class HasManyBugTest < MiniTest::Unit::TestCase def setup @suite = Suite.new end def test_order assert @suite.tenancies.to_sql.include? "ORDER BY tenancies.started_at DESC" end def test_order_on_through_relation assert @suite.tenants.to_sql.include? "ORDER BY tenancies.started_at DESC" end end