Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, order by nested value

I have this classes:

class Fruit
 attr_accessible :name, :position
 has_many :apples
 default_scope order('position ASC')
end

class Apple
 attr_accessible :name
 belongs_to :fruit
end

How sort apples by fruit.position?

like image 386
Alexander Shlenchack Avatar asked May 11 '13 21:05

Alexander Shlenchack


2 Answers

Sorry guys, I think it must be like this: @apples.joins(:fruit).order("fruits.position")

like image 99
Alexander Shlenchack Avatar answered Nov 20 '22 17:11

Alexander Shlenchack


You can do it like this:

@apples.joins(:fruit).order(Fruit.arel_table[:position])

like image 29
Matt Avatar answered Nov 20 '22 16:11

Matt