简体   繁体   English

原始SQL到Rails ActiveRecord

[英]Raw SQL to Rails ActiveRecord

Because I'm newer to Rails than I am SQL, I took a shortcut in order to build a fairly complicated (to me, anyways) query. 因为我比SQL陌生于Rails,所以我采用了一条捷径以构建一个相当复杂的查询(对我而言)。

Good news: I have the query working 好消息:我正在执行查询

Bad news: I'm stuck with hackish code AND can't figure out how to drop dynamic variables into it (which should be easy if it as in Rails-friendly format). 坏消息:我受制于乱码,无法弄清楚如何将动态变量放入其中(如果采用Rails友好格式,这应该很容易)。

Here's my query (broken up for ease of reading): 这是我的查询(为便于阅读而分解):

ActiveRecord::Base.connection.select_all( "SELECT DISTINCT ps.* FROM merchants merch, product_sales ps, users u, user_merchant_relations umr, memberships m

INNER JOIN marketing_efforts me ON me.id = ps.marketing_effort_id

LEFT OUTER JOIN member_sales ms ON me.id = ms.marketing_effort_id

LEFT OUTER JOIN crm_sales cs ON me.id = cs.marketing_effort_id

WHERE ((me.start < '2013-05-26' AND me.end > '2013-05-26') OR (me.start < '2013-05-26' AND me.end IS NULL)) AND (me.num_avail IS NULL OR me.num_avail > 0) AND (me.gender IS NULL OR me.gender = u.gender OR u.gender IS NULL) AND ((u.birthdate > me.birthdate_start AND u.birthdate < me.birthdate_end) OR (u.birthdate > me.birthdate_start AND me.birthdate_end IS NULL) OR (me.birthdate_start IS NULL AND me.birthdate_end IS NULL) OR u.birthdate IS NULL) AND ((ms.member_id = m.member_id AND m.user_id = u.id) OR ms.member_id IS NULL) AND (cs.crm_tier_id = umr.crm_tier_id OR cs.crm_tier_id IS NULL) AND u.id = 3 AND merch.id = 1")

Anywhere '2013-05-26' appears I need to drop in Time.now. 在出现“ 2013-05-26”的任何地方,我都需要放入Time.now。 The u.id and merch.id at the very end also need to accept variables. 最后的u.id和merch.id也需要接受变量。

I thought I could simply do: .S=?...Q=?....L=?...", value, value, value) but that's not working. 我以为我可以做:.S =?... Q =?.... L =?...”,值,值,值),但这不起作用。

Update 更新资料

I'm here: 我在这:

ProductSale.joins('INNER JOIN marketing_efforts ON marketing_efforts.id = product_sales.marketing_effort_id').joins('LEFT OUTER JOIN member_sales ON marketing_efforts.id = member_sales.marketing_effort_id').joins('LEFT OUTER JOIN crm_sales ON marketing_efforts.id = crm_sales.marketing_effort_id')

So I'm taking care of all the joins, but the problem I'm running into is that, with the raw SQL, I could easily define all the tables I need to access ... eg: 所以我要处理所有的联接,但是我遇到的问题是,使用原始SQL,我可以轻松定义所有需要访问的表...例如:

FROM merchants merch, product_sales ps, users u, user_merchant_relations umr, memberships m

Whereas I'm not sure how to do this with Rails. 而我不确定如何使用Rails做到这一点。 As a result, I just get FROM product_sales : 结果,我刚从FROM product_sales获得:

ProductSale Load (0.3ms) SELECT "product_sales".* FROM "product_sales" INNER JOIN ....

So when I try to say, for example, users.id = 1 I get: 因此,当我尝试说例如users.id = 1我得到:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.id:

Any thoughts? 有什么想法吗? Thank you. 谢谢。

也许这不是最好的方法,但是它可以解决主要问题(无法传递动态变量):

ProductSale.find_by_sql [ " QUE=?...R=?..Y=? ", value, value, value]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM