On Nov 4, 4:05 pm, JavierQQ <[email protected]> wrote:
>
> I want that the data validates in 2 cases (create / update)
> because when I want to update , for example:
>
> Evaluation 1
> - Question 1
>
> If I update question 1 and change the content, and then I save
> it shows a error message that says "The number is being used" (and
> that shows that
>  validates_uniqueness_of :qnumber, :scope  works)
> I want that it only validates it when I'm CREATING a new one, not when
> I'm UPDATING
>

validations are fundamentally ignorant of what it is that has caused
save to be called (i.e. it doesn't know what action, controller etc.
There might not even be a controller if the code was being run from a
background task).

The validation methods do take an on option (:on => :create or :on
=> :update) but you should understand that this refers to whether
active record is create or updating an object and has nothing to do
with what controller action was involved.

However, validates_uniqueness_of should already be adding a condition
to the query so that when updating an existing object it doesn't find
itself, so I'm not sure why you're running into this

Fred

> How can I do that?
>
> On 4 nov, 10:55, Colin Law <[email protected]> wrote:
>
>
>
>
>
>
>
> > On 4 November 2011 15:40, JavierQQ <[email protected]> wrote:
>
> > > Hi
>
> > > I hope someone can help me with this
>
> > > I have a model Question, and its controller .. what I've done to
> > > validate that there's no evaluation with 2 question with the same
> > > number is
>
> > >          validates_uniqueness_of :qnumber, :scope
> > > => :evaluation_id
>
> > > but when I try to update a question's content I got an error message
> > > that says that "#{qnumber} has already been taken"
> > > and that means that the validation works, but not as I want
>
> > > Can I obtain wich "def" is using ? because I want to do this
> > > - Question Model
>
> > >           IF ("def create" or something)
> > >                 do the validation
> > >           elsif ("def update")
> > >                 don't do it
> > >          end
>
> > I don't know what you mean by "def" but if you want to do conditional
> > validations have a look at the Rails Guide on validations and it will
> > show you how.
>
> > Colin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to