[英]Rails How do you get the name of the text box into the database instead of the boolean value?
[英]In Rails, how do you get a variable that holds a database field name into a condition?
以下控制器代碼不起作用。 有辦法嗎?
@m = "jan"
@teams = Team.find(:all, :conditions => ["@m = ?", true], :order => "name asc")
謝謝。
如果使用Rails 3.x
@teams = Team.where(@m => true).order("name asc").all
您需要插入字符串!
@m = "jan"
@teams = Team.find(:all, :conditions => ["#{@m} = ?", true], :order => "name asc")
順便說一下,檢查布爾值時的數據庫查詢應使用IS運算符
@m = "jan"
@teams = Team.find(:all, :conditions => ["#{@m} IS ?",true], :order => "name asc")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.