与has_many_through关系的关联排序

时间:2013-09-04 03:50:22

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 has-many-through

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

  

当我这样做时,我希望通过价格获得排序顺序的结果   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    |                |
+------------+--------------+------+-----+---------+----------------+

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

0 个答案:

没有答案