In my Rails app, I need to store boolean fields in a model. Now, this causes a problem since Sqlite3 (which I use for development) uses the character constants 't' and 'f' for truth values. MySQL however, to which my app will be deployed, uses the literals true and false (corresponding to tinyints 1 and 0 respectively). This makes the use of SQL conditions problematic, since an expression of the type
select * from articles where published = true
expressed in Rails as
Article.where 'published = true'
will fail in Sqlite3 with the message no such column: true.
What is the preferred way to handle booleans adapter-portably in Rails?
Using active record, you would do
You would probably want to take this a step further, and make this a scope
So that in your controller you can just go
More information on active_record’s query interface here: http://guides.rubyonrails.org/active_record_querying.html
You are also using an assignment operation
=instead of equality comparison==