Rails的first_or_create增加(1 = 2)查询 [英] Rails first_or_create adds (1=2) to query

查看:524
本文介绍了Rails的first_or_create增加(1 = 2)查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管通过沙盒模式Heroku的控制台上运行,开发code,我用first_or_create来测试存在的一个纪录:

While running development code through Heroku console in sandbox mode, I use first_or_create to test for existence of a record:

Right.where(:language           => language          ).
      where(:work_id            => work_id           ).
      where(:contact_id         => contact_id        ).
      first_or_create!

要测试存在记录的查询获得一个额外的predicate(1 = 2)添加到它,所以没有找到记录。

The query to test for existence of the record gets an extra predicate (1=2) added to it, so the record is not found.

SELECT "rights".* FROM "rights" WHERE "rights"."language" = 'ger' AND "rights"."work_id" = 625 AND "rights"."contact_id" = 1435 AND (1 = 2) LIMIT 1

任何人都可以建议我如何追查源本 - ?它是一个沙盒模式的东西,也许

Can anyone suggest how I track down the source of this -- is it a sandbox mode thing, perhaps?

编辑:沙盒模式由调用:

Sandbox mode being invoked by:

heroku run console -s --app my-app-name

<一个href="http://stackoverflow.com/questions/15527383/running-rails-console-on-heroku-in-sandbox-mode">Running Rails的控制台在Heroku上,在沙盒模式

推荐答案

确定,解开了谜底。

该first_or_create正在从成参数传递的一些属性的方法调用。是这样的:

The first_or_create was being invoked from a method into which arguments were passed for a number of attributes. Something like:

def get_right(language,work_id,contact_id,terms)
  right = Right.where(:language           => language          ).
                where(:work_id            => work_id           ).
                where(:terms              => terms             ).
                where(:contact_id         => contact_id        ).
                first_or_create!
  right.id
end

在该方法被调用的参数进行传递,而不是零为{}条款。

When the method was called the terms argument was being passed as {} instead of nil.

显然处理愚蠢像那样的activerecord的方法是消除在查询违规柱(计算)predicate和附加一个(1 = 2)predicate代替。

Apparently activerecord's method of dealing with stupidity like that is to remove the predicate on the offending column (terms) in the query and append a (1 = 2) predicate instead.

我不能说我不会,而遇到一个错误:(

I can't say that I wouldn't rather encounter an error :(

这篇关于Rails的first_or_create增加(1 = 2)查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆