how to fetch value of a column which has some default value rails mysql

187 views Asked by At

I had a User model, with attributes email, name ... I wanted to add a column has_agreed with a default value true. So I added that migration and ran it, it shows properly in the database. Now when I fetch the value, @user.has_agreed, it does not return me any value. I have written has_agreed in the attribute accessible (Rails 3.2). If I manually change the contents of the database by an update command for this particular user to change has_agreed value to false for user with id 1 and then run the same call, @user.has_agreed, it returns false.

I have historical data and I want to add this default column and can not and do not intend to manually update this column through database. How to get the value?

Also, the issue is with only the old users in the user table. For the new users it works fine.

1

There are 1 answers

0
kor On

in user.rb

class User def has_agreed # use this line code. if has_agreed not equal false # read_attribute(:has_agreed) || default_velue if read_attribute(:has_agreed).blank? ## default value true else read_attribute(:has_agreed) end end end