0

我想使用价格获得排序结果。

我想得到按价格排序的结果 hotel.packages

我有两个(hotel and package )has_many :through关系的模型。两者都通过关系相互连接,其模型名称为package_price

酒店型号为:

 class Hotel < ActiveRecord::Base  
    has_many :package_prices, :dependent => :destroy 
    has_many :packages, :through => :package_prices, :order => 'package_prices.price'
  end

封装型号为:

class Package < ActiveRecord::Base
    has_many :package_prices, :dependent => :destroy #foreign key package_id
    has_many :hotels, :through => :package_prices, :order => 'package_prices.price'
end

package_price 模型是:

class PackagePrice < ActiveRecord::Base
  belongs_to :package
  belongs_to :hotel
end

package_prices 表字段:

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| package_id | int(11)      | YES  |     | NULL    |                |
| price      | int(11)      | YES  |     | NULL    |                |
| hotel_id   | varchar(255) | YES  |     | NULL    |                |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

我想按价格排序获得酒店套餐。

4

0 回答 0