我有三个模型,,,Hotel
和Package
关联PackagePrice
是这样的:
酒店可以有很多套餐,每个套餐有一个套餐价格
型号有:
class Hotel < ActiveRecord::Base
attr_accessor :excel_sheet, :excel_sheet_file_name
attr_accessible :hotel_name, :stars, :location, :searchable, :excel_sheet, :excel_sheet_file_name
has_many :package_prices, :dependent => :destroy
has_many :packages, :through => :package_prices, :order => 'package_prices.price'
end
class Package < ActiveRecord::Base
attr_accessible :package_name
has_many :package_prices, :dependent => :destroy, :order => 'price DESC'
has_many :hotels, :through => :package_prices, :order => 'package.package_prices.price'
end
class PackagePrice < ActiveRecord::Base
attr_accessible :price, :package_id, :hotel_id
belongs_to :package
belongs_to :hotel
end
相应的表格是:
mysql> desc hotels
-> ;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| hotel_name | varchar(255) | YES | | NULL | |
| stars | varchar(255) | YES | | NULL | |
| location | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| searchable | tinyint(1) | YES | | 1 | |
+------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc packages;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| package_name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc 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 | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| hotel_id | int(11) | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
我在一个变量中有酒店记录,@hotels
我想@hotels
按price
顺序过滤它asc or desc
。所以请帮我找出这个查询。