Reputation: 94
there are other questions just like mine here, but none of them actually work for me.
I have a Rails project, with 3 models: Unity, City, State, which have their relationships as below:
class City < ApplicationRecord
belongs_to :state
has_many :unities
end
class State < ApplicationRecord
has_many :cities
end
class Unity < ApplicationRecord
belongs_to :city
belongs_to :state, through: :city
end
I wish I could be able to order my unities with respect to their city.name or state.name, as well as to order the cities according to their state.name. But I just can't figure it out. I tried, for example, City.all.joins(:state).order('state.id')
as suggested in some question here at StackOverflow, but it gives me missing FROM clause entry for 'city'
. How could I make it?
Upvotes: 0
Views: 60
Reputation: 925
It should be order('states.id')
instead of order('state.id')
. Order clause expects table name not the association name.
Upvotes: 1