Ruby 代码作为 dbase 的属性
我正在建立一个项目,我想将一些 Rails 代码移动到我的 mysql 数据库。我想将控制器中使用的条件放入数据库中。我将此表称为“过滤器”。属性是名称和过滤器,在本例中我将条件放在一个简单的条件中:'status = ?' ,'P'
。
现在,我想从我的控制器中调用这样的条件:
@programs = Program.where(Filter.find_by_name(params[:filter]).filter)
因此,根据 de url 中的过滤器参数,我调用该条件。我无法让它工作,我认为问题在于条件以字符串形式返回。所以基本上我最终得到:
@programs = Program.where("'status = ?' ,'P'") # including the string quotes
返回一个错误:
ActiveRecord::StatementInvalid: Mysql2::Error: Operand should contain 1 column(s): SELECT `programs`.* FROM `programs` WHERE ('status = ?' ,'P')
这确实有效:
@programs = Program.where('status = ?' ,'P')
所以我的条件、控制器和模型都很好。有人知道如何解决这个问题吗?
I'm setting up a project where I want to move some of the rails code to my mysql database. I want to put conditions that I use in the controller in the database. I have called this table Filter. The attrbutes are name and filter, in which I put the condition in this case a simple one: 'status = ?' ,'P'
.
Now from my controller I want to call the condition like this:
@programs = Program.where(Filter.find_by_name(params[:filter]).filter)
So, based on the filter parameter in de url I call the condition. I can't get it to work and I think the problem is that the condition is returned as a string. So basically I end up with:
@programs = Program.where("'status = ?' ,'P'") # including the string quotes
That returns an error:
ActiveRecord::StatementInvalid: Mysql2::Error: Operand should contain 1 column(s): SELECT `programs`.* FROM `programs` WHERE ('status = ?' ,'P')
This does work:
@programs = Program.where('status = ?' ,'P')
so my condition, the controller and the model are fine. Anybody know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将其存储在数据库中:
这将变为:
并且应该可以正常工作。
Just store this in the database:
This becomes:
and should work fine.