Monday, August 15, 2011

Re: [Rails] newbie: before_create :setuniqueno

> i was wondering how this should be handled.imagine i have this class:
>
> class Invoice < ActiveRecord::Base
> before_create :setuniqueno
> validates_uniqueness_of :uniquenumber
> def setuniqueno
> self.uniquenumber = Time.now.to_i
> end
> end
> ######
> class Task<ActiveRecord::Base
> end
> ######
> after the user post to the create function i also create an Invoice.
> what is not 100% clear to me, is what happens/how should i handle the
> case if the uniquenumber is not unique? eg if multiple user generate
> the same Timestamp
> how does the task-controller get feedback from the Invoice-Model?
> im doing a find_or_create_by in the create action of the task.

In your controller, assuming you have an @invoice variable you can call @invoice.valid? on it to see if it was found/created successfully.

-philip

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate