ruby on rails - Complex sorting on association in rails3 -
i have 3 model hotel
, package
, packageprice
, , association like:
hotel can have many packages , each package has 1 packageprice
models are:
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
and corresponding tables are:
mysql> desc hotels -> ; +------------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +------------+--------------+------+-----+---------+----------------+ | 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 | | +--------------+--------------+------+-----+---------+----------------+ | 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 | | +------------+----------+------+-----+---------+----------------+ | 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 | | +------------+----------+------+-----+---------+----------------+
and have hotels records in variable @hotels
, want filter @hotels
price
in asc or desc
order. please me find out query.
you using this,
@hotels = hotel.joins(:package_prices).order('package_prices.price')
however don't think models set way described, i'm not sure if want
Comments
Post a Comment