【发布时间】:2012-10-11 13:53:22
【问题描述】:
我有一个字符串变量如下:
the_style = @house.style # this is a string variable and contains the value "modern"
如果我在数据库中搜索如下:
Building.find(:last, :conditions => [" style = ?", "#{the_style}" ])
我收到上述错误,但是如果我在字符串值中硬编码它可以工作:
Building.find(:last, :conditions => [" style = ?", "modern" ])
将字符串变量放入查找条件的正确方法是什么?
【问题讨论】:
-
你得到什么错误?你用的是什么版本的 Rails?你正在尝试的应该工作,所以你的变量一定有问题。只是一个小注释:
"#{the_style}"与the_style相同。像这样将变量放在引号中是没有用的。
标签: ruby-on-rails string conditional-statements