开发者

MetaWhere to Squeel migration

In MetaWhere I combined conditions to sql variable using loops, if else statements.

sql = {}
email_starts_with = "vany%"
sql["growth"] = 0..200
sql = sql & (:rating > 50)
sql = sql & (:email =~ email_starts_with)
.....
.....
User.where(sql).to_sql
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"growth\" BETWEEN 0 AND 200 AND    \"users\"开发者_如何学Go.\"rating\" > 50 AND \"users\".\"email\" ILIKE 'vany%'"
user = User.where(sql).first
=> #<User id: 1, .................................. >

How can I do the same using Squeel?

Thanks for any help)


check out the "squeel" method, new in 0.9.0. It was added to support exactly this sort of thing. It just gives you an easy way to write a block of Squeel DSL without actually attaching it to a "where", "join", etc.

You also might want to consider encapsulating this logic in a sifter for your model.

class User < ActiveRecord::Base
  sifter :my_sifter do |growth_range, min_rating, email_start|
    growth.in(growth_range) & rating.gt(min_rating) & email.matches("#{email_start}%")
  end
end

User.where{sift :my_sifter, 0..200, 50, 'vany'}


I resolved my problem using Arel. I think (I just changed MetaWhere code to Arel) that it is just the same as MetaWhere.

t = User.arel_table

email_starts_with = "vany%"
sql = t[:rating].gt(50)
sql = t[:growth].in(0..200)
sql = sql.and(t[:email].matches(email_starts_with))

User.where(sql).to_sql
=> "SELECT \"users\".* FROM \"users\"  WHERE (\"users\".\"growth\" BETWEEN 0 AND 200 AND \"users\".\"email\" ILIKE 'vany%')"

Thanks everybody for help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜