Leo
Leo

Reputation: 311

How to set boolean value before save to database?

Say I have a Item model, which has a boolean onshelf.

I want to explicitly set onshelf value to true by doing something like self.onshelf = true. But I learn in the book Agile Web Development that, different database stores boolean value quite differently, and I'm afraid my practice might break my app in the future.

So I want to know, if I set boolean like self.onshelf = true, will Rails performs type-conversion (correctly)?

I know that toggle! method can do this too, but I don't like it to immediately save the result. I want to set every attribute first, then let after_save decides.

Thanks!

Upvotes: 1

Views: 2257

Answers (2)

seand
seand

Reputation: 5286

ActiveRecord (with its database drivers) will store boolean's in a way most appropriate for the particular database you're using.

Upvotes: 1

madth3
madth3

Reputation: 7344

ActiveRecord should convert the ruby boolean to the closest thing in the corresponding DB.

Upvotes: 1

Related Questions