SQL到RAILS3使用Active记录?

时间:2011-10-21 09:07:24

标签: ruby-on-rails-3

任何人都可以帮我用rails activerecord query>来写这个

SELECT DISTINCT `merchants`.id FROM `merchants` 
  INNER JOIN `merchant_status_assignments` ON `merchants`.`id` = `merchant_status_assignments`.`merchant_id` 
  INNER JOIN `merchant_statuses` ON `merchant_statuses`.`id` = `merchant_status_assignments`.`merchant_status_id` 
  INNER JOIN `merchant_status_assignments` `merchant_status_assignments_merchants` ON `merchant_status_assignments_merchants`.`merchant_id` = `merchants`.`id` 
  LEFT OUTER JOIN `favourite_merchants` ON `favourite_merchants`.`merchant_id` = `merchants`.`id` AND `favourite_merchants`.`customer_id` = 11
  WHERE `merchants`.`status` = 1 AND `merchant_statuses`.`name` = 'new_merchant' 
    AND (merchants.site_id = 1)

1 个答案:

答案 0 :(得分:0)

我得到了答案

控制器

DEF new_list @merchants = Merchant.front_search(current_site.id,params).includes(:favourite_merchant).where(customer_id => current_customer.id) END

模型

DEF self.front_search(site_id,params)商家= merchants.joins(:merchant_statuses,:merchant_status_assignments).where(“merchant_statuses.name”=> params [:status])。select('DISTINCT merchants。*') 结束