Hi Pietro,

  yes, this is an interesting misunderstanding. When we study Ruby, we may
have a class Player, and when we do:

   lionel = Player.new(:name => "messi", :team => "barcelona", ...)

we say that we have *created* an object of the class Player.

But it is different in ActiveRecord (AR); the word *creation* means "*creating
in the database*". AR does not care about much about our u=User.new(); but
it cares when we save that user u! that is the moment of the "creation"...

So if you have:

  validates_presence_of  :name, :on => :create
  u1  = User.new(:name => "adriano", :hobby => "singer", ...)   # no
validation
  u1.save   # => true  (*validation ok because :name is present*)

# but now, after u1 was created succesfully, we could do:
  u1.name = ""
  u1.save  # => true (*because validation is not done*!)

If instead I had written:   validates_presence_of  :name
then the second save would have been "false" (as the validation would now
always be done).
Of course, most of the times one does not specify "on :create" (as usually a
certain condition on an attribute must always be satisfied); but this is
another matter.

To summarize, there are 2 points that you have to keep in mind:
a) there is NO validation done at "User.new"
b) when we say "create", we mean the operations:
    User.create(...)
    # or
    u = User.new(...);  u.save

I hope that this cleared up things

Raul


On Tue, Oct 28, 2008 at 11:23 PM, Pietro Maggi <[EMAIL PROTECTED]>wrote:

> On Wed, Oct 29, 2008 at 12:39 AM, raul parolari <[EMAIL PROTECTED]>
> wrote:
> >
> [snip]
> > In any case, I don't understand the final comment when you say that you
> are
> > stuck. Let me know what the problem is, so that I can help
> >
> Hi Raul,
> I'm a bit short of time and this can only have bad result on my English ;-)
> But, what I was referring to with the "I'm stuck" comment was due to a
> misunderstanding I have with the validation parameter ":on=>:create".
> Initially I thought that this can instruct the AR to validate the
> object on the Creation:
> user = User.new() <--- this call validation with ":on=>:create"
> user.save <-- this call validation with default or ":on=>:save"
>
> well... obviously I was wrong, but it takes me some time to understand
> what the ":on=>:create" does.
>
> Best regards, and thanks again for the conversation.
>
> Pietro
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to