在 Rails 3 中重构 activerecord 方法

发布于 2024-11-24 07:25:16 字数 1249 浏览 8 评论 0原文

这个事件方法可以重构和简化吗?

class Manager < User
  has_and_belongs_to_many :customers

  def events
    Event.joins(:customers => :managers).where(:users => { :id => self }).select("DISTINCT(events.id), events.description, events.created_at")
  end

end

我希望我可以在我当前拥有的 Manager 实例之上构建查询,但似乎无法做到这一点。我尝试了以下操作,但出现错误

def events
  customers.joins(:events).select("DISTINCT(events.id), events.description, events.created_at")
end

current_user.events

但这会导致 MySQL 错误:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT(events.id), events.description, events.created_at FROM `customers` INNER' at line 1: SELECT `customers`.*, DISTINCT(events.id), events.description, events.created_at FROM `customers` INNER JOIN `customers_events` ON `customers_events`.`customer_id` = `customers`.`id` INNER JOIN `events` ON `events`.`id` = `customers_events`.`event_id` INNER JOIN `customers_managers` ON `customers`.`id` = `customers_managers`.`customer_id` WHERE `customers_managers`.`manager_id` = 27 ORDER BY created_at DESC

Can this events method be re-factored and simplified?

class Manager < User
  has_and_belongs_to_many :customers

  def events
    Event.joins(:customers => :managers).where(:users => { :id => self }).select("DISTINCT(events.id), events.description, events.created_at")
  end

end

I was hoping I could build the query on top of the Manager instance I currently have, but seem unable to do this. I tried the following, but got an error

def events
  customers.joins(:events).select("DISTINCT(events.id), events.description, events.created_at")
end

and

current_user.events

But this results in MySQL error:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT(events.id), events.description, events.created_at FROM `customers` INNER' at line 1: SELECT `customers`.*, DISTINCT(events.id), events.description, events.created_at FROM `customers` INNER JOIN `customers_events` ON `customers_events`.`customer_id` = `customers`.`id` INNER JOIN `events` ON `events`.`id` = `customers_events`.`event_id` INNER JOIN `customers_managers` ON `customers`.`id` = `customers_managers`.`customer_id` WHERE `customers_managers`.`manager_id` = 27 ORDER BY created_at DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-12-01 07:25:16

根据MySQL手册,DISTINCT关键字必须出现在select_expr:

SELECT foo.*, DISTINCT(foo.id) 

会给你这个错误。

SELECT distinct(foo.id), foo.*

会起作用的。试试这个:

customers.select("DISTINCT(events.id), events.description, events.created_at").joins(:events)

但是,如果您想做的只是获取每个用户的唯一事件(我认为这就是 DISTINCT 的用途),您可以使用

current_user.events.group("events.id")

According to the MySQL manual, the DISTINCT keyword must come before the select_expr:

SELECT foo.*, DISTINCT(foo.id) 

will give you that error.

SELECT distinct(foo.id), foo.*

will work. Try this:

customers.select("DISTINCT(events.id), events.description, events.created_at").joins(:events)

However, if all you're trying to do is to get the unique events per user (I assume that's what the DISTINCT is for), you could just use

current_user.events.group("events.id")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文