LoginSignup
2
2

More than 5 years have passed since last update.

AR 4.0.1 で joins(:m) + M.order(:x => :asc) 形式に注意

Posted at
require "active_record"
ActiveRecord::VERSION::STRING   # => "4.0.1"
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
class User < ActiveRecord::Base
  has_many :items
end
class Item < ActiveRecord::Base
end

User.joins(:items).merge(Item.order(:x)).to_sql[/BY.*/]                      # => "BY \"items\".\"x\" ASC"
User.joins(:items).merge(Item.order(:x => :asc)).to_sql[/BY.*/]              # => "BY \"users\".\"x\" ASC"
User.joins(:items).merge(Item.order(Item.arel_table[:x].asc)).to_sql[/BY.*/] # => "BY \"items\".\"x\" ASC"

2つめの書き方だと items.x のはずが users.x になります。
かなりはまりました。
masterブランチだと直ってました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2