[英]In Rails, how do you get a variable that holds a database field name into a condition?
The following controller code doesn't work. 以下控制器代码不起作用。 Is there a way? 有办法吗?
@m = "jan"
@teams = Team.find(:all, :conditions => ["@m = ?", true], :order => "name asc")
thanks. 谢谢。
如果使用Rails 3.x
@teams = Team.where(@m => true).order("name asc").all
You need to interpolate the string! 您需要插入字符串!
@m = "jan"
@teams = Team.find(:all, :conditions => ["#{@m} = ?", true], :order => "name asc")
And by the way, database queries when checking for booleans should use the IS operator 顺便说一下,检查布尔值时的数据库查询应使用IS运算符
@m = "jan"
@teams = Team.find(:all, :conditions => ["#{@m} IS ?",true], :order => "name asc")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.