mah*_*ahu 2 ruby sql ruby-on-rails where relation
我在构建我.where
以检索特定值时遇到了麻烦.
我有这个层次结构:
-顾客
--has_many项目
--- has_many门票
我想通过客户ID检索所有票证作为ActiveRecord :: Relation.
我的想法是这个循环(c是我想要门票的客户):
customer_projects = Project.where(:customer_id => c.id)
tickets = ActiveRecord::Relation.new(Ticket, anything)
customer_projects.each do |cp|
project_tickets = Ticket.where(:project_id => cp.id).where("DATE(created_at) >= ?", report.start_time).where("DATE(created_at) <= ?", report.end_time)
tickets.insert(project_tickets)
end
Run Code Online (Sandbox Code Playgroud)
我不知道在哪里写"任何"作为表参数,也不知道这是否有效.我更喜欢.where
可以取回所有门票的"简单" .
你不需要这么麻烦.你有through
办法为你做这件事.
# customer.rb
has_many :tickets, through: projects
# ticket.rb
has_many :customers, through: projects
Run Code Online (Sandbox Code Playgroud)
这样,您可以:
@customer.tickets
@ticket.customers
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26 次 |
最近记录: |