简体   繁体   English

在Rails中,如何获取将数据库字段名称保存为条件的变量?

[英]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.

相关问题 Rails如何将文本框的名称而不是布尔值输入数据库? - Rails How do you get the name of the text box into the database instead of the boolean value? 如何在Ruby on Rails中获得转发的URL的域名? - How do you get the domain name of a forwarded URL in Ruby on Rails? 如何在Heroku上运行Rails时获取应用程序名称? - How do you get the app name when running Rails on Heroku? 在rails 4的where条件中使用字符串变量作为字段名称 - Use string variable as field name in where condition in rails 4 rails:当您以html形式输入一个字段时,如何计算第二个字段并将其存储在数据库中呢? - rails: How to do that when you enter one field in a html form, the second field is calculated and stored in a database? 您如何按 Rails 中每个游戏字段的名称对锦标赛游戏进行排序? - How do you order a tournaments games by the name of each games field in Rails? 你能在 Rails 中获取数据库用户名、密码、数据库名称吗? - Can you get DB username, pw, database name in Rails? 如何根据'created_at'字段的月份设置条件 - How do you set a condition based on the month of the 'created_at' field Rails-如何从数据库中获取类别名称? - Rails - how to get category name out of database? 如何获取存储在SQL数据库中的图像并将其显示在转盘中? (Ruby on rails) - How do you get an image stored in a SQL Database and display it in a carosel? (Ruby on rails)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM