Question

I have a model Vote. Vote has an attribute vote_type. In my controller I do this

 @vote = Vote.fetch(1)
 logger.warn("current vote_type: #{@vote.vote_type}")
 @vote.vote_type = false 
 @vote.save

This doesn't throw any error but I see the following in my server

    Vote Columns (0.7ms)   SHOW FIELDS FROM `votes`
    current vote_type: true
    SQL (0.1ms)   BEGIN
    SQL (0.1ms)   ROLLBACK

I have no idea why it is rolling back. What am i doing wrong?

Was it helpful?

Solution

If the field can 'not have a value' - which is what false is - then remove the required => true from the original migration and recreate that table/db (or create a new migration that removes the required setting).

OTHER TIPS

A common issue with Rails is trying to 'validate_presence' of a boolean. That validation uses .blank? and false.blank => true. Therefore, to validate the presence of a boolean, use:

validates :vote_type, :inclusion => {:in => [true, false]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top