Ruby code as attribute from dbase
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 st开发者_运维技巧ring 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?
Just store this in the database:
status = 'P'
This becomes:
@programs = Program.where("status = 'P'")
and should work fine.
精彩评论