I was working yesterday and realized that I couldn’t find how a model of
mine was getting a default value:
part = Part.new
part.type # 1
I scoured my model and its observer, but such a default had not been
programmed. Then I checked the schema, and there it was: :default => 1.
So what’s your take? How do you feel about defining defaults in the
database? If you define a default in the database should you validate
the attribute anyway? Should you double-enforce the default in your
model? What do you do?
database? If you define a default in the database should you validate
the attribute anyway? Should you double-enforce the default in your
model? What do you do?
Do it in the database. That way in a year when you run some SQL
manually
via some other means “just this one time” you won’t shoot yourself in
the
foot.
Wouldn’t hurt to clarify it in the model as well though or make a note
of
it perhaps so a year from now you remember where that’s coming from…
database? If you define a default in the database should you validate
the attribute anyway? Should you double-enforce the default in your
model? What do you do?
Do it in the database. That way in a year when you run some SQL
manually via some other means “just this one time” you won’t
shoot yourself in the foot.
That’s a fair point.
In this particular situation though the value being defaulted is a
foreign key. It’s a default, but with an assumption that there’s a
record in another table with an ID of 1. So it feels like that breaks
any kind of safety it tried to create.
I guess I can agree with defaults that aren’t foreign keys and are
documented along with the class.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.