Ruby on rails 如何使用Rails 5在控制器内按属性排序has_one关系的子级?
我小时候有两个模特,一个是这样的关系:Ruby on rails 如何使用Rails 5在控制器内按属性排序has_one关系的子级?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我小时候有两个模特,一个是这样的关系: class HotelThai < ApplicationRecord belongs_to :hotel #validates :hotel_id, presence: true validates :hotel_name, presence: true validates :description, presence: true end class HotelEng < ApplicationRecord be
class HotelThai < ApplicationRecord
belongs_to :hotel
#validates :hotel_id, presence: true
validates :hotel_name, presence: true
validates :description, presence: true
end
class HotelEng < ApplicationRecord
belongs_to :hotel
validates :hotel_name, presence: true
validates :description, presence: true
end
但我得到的是:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "hotel_eng" LINE 1: ...tels" WHERE "hotels"."is_highlight" = $1 ORDER BY hotel_eng.... ^ : SELECT "hotels".* FROM "hotels" WHERE "hotels"."is_highlight" = $1 ORDER BY hotel_eng.hotel_name ASC LIMIT $2 OFFSET $3
那么,有人知道如何解决这个问题,或者知道如何用其他方法解决这个问题吗?如果您想使用order by the columns,您需要连接相关的表。为此使用联接或包含方法
def index
hotel = I18n.locale == :th ? :hotel_thai : :hotel_eng
@normal_hotel = Hotel.where(is_highlight: false).
joins(hotel).order("#{hotel.to_s.pluralize}.hotel_name ASC").
paginate(page: params[:page], per_page: 10)
normal_hotel_group = @normal_hotel.in_groups(2)
...
end
在订单状态下使用多元化方法
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "hotel_eng" LINE 1: ...tels" WHERE "hotels"."is_highlight" = $1 ORDER BY hotel_eng.... ^ : SELECT "hotels".* FROM "hotels" WHERE "hotels"."is_highlight" = $1 ORDER BY hotel_eng.hotel_name ASC LIMIT $2 OFFSET $3
def index
hotel = I18n.locale == :th ? :hotel_thai : :hotel_eng
@normal_hotel = Hotel.where(is_highlight: false).
joins(hotel).order("#{hotel.to_s.pluralize}.hotel_name ASC").
paginate(page: params[:page], per_page: 10)
normal_hotel_group = @normal_hotel.in_groups(2)
...
end