In the migration file I have:
class AddEnabledToJob < ActiveRecord::Migration
def change
add_column :jobs, :enabled, :boolean, default: 0
end
end
But then in my view I'm looking for every :enabled => false
object:
<% @jobs.where(enabled: false).order("id DESC").each do |job| %>
This doesn't work. It works if I change it to:
<% @jobs.where(enabled: 0).order("id DESC").each do |job| %>
And this is confusing since I'm changing the enabled
value to false
somewhere else in my application so I have some Job objects with enabled: false
and some with enabled: 0
. How can I correct this and why does it happen?
EDIT:
To expand on this issue I have populated all Jobs ( Job.all
) via the console and I see that the while the new records are created with default: 0
they have a value of false
. So why is @jobs.where(enabled: false)
failing to retrieve them?
use change_column_default
to change default to false, and after allow each 0 to be false for old records:
def up
change_column_default :jobs, :enabled, false
Job.where(enabled: 0).update_all(enabled: false)
end
def down
change_column_default :jobs, :enabled, 0
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.