On Jun 18, 2:20 am, David Lee <[EMAIL PROTECTED]> wrote:
> class Post < Sequel::Model
>   belongs_to :user
>
>   validates_presence_of :email
>   validates_presence_of :hidden
> end
>
> post = Post.new :user => User.create
> post.valid? #=> false, that's good
> post.hidden = false
> post.valid? #=> false, should be true

That has always annoyed me about validates_presence_of.  With
ActiveRecord, I always ended up using validates_inclusion_of with true
and false.

> Upon fixing this, I suggest that Model#create and Model#save return a
> nil instead of a false on an unsuccessful save because:

That will complicate things for save_changes.  Currently, save_changes
returns nil if no columns changed, and false if it didn't save (due to
validation problems or a before_ hook returning false). I suppose we
could just change save_changes to return false if save returns nil,
and nil if it doesn't attempt to save.  This breaks backwards
compatibility slightly though, for people expecting save to return
false instead of nil.

> post = Post.new :public => false
> post.valid? #=> false, that's good
> post.user => User.create
> post.valid? #=> true, even if User.create failed and post.user == false

I see 4 possible options:

1) We could use the db_schema to see if the column is a boolean and
consider false as present just for booleans
2) We could add validates_inclusion_of:
validates_inclusion_of :hidden, :in=>[true, false]
3) We could add validates_boolean: validates_boolean :hidden
4) We could modify save to return nil and always consider false as
present, and special case save_changes

I'm open to other ideas and/or a patch in this area.  I don't think
option 1) is a good idea, as the db_schema isn't always available.  I
think 4) may be the best bet in the long run.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to