re-ordering for top -> bottom

On Nov 4, 2011, at 9:55 AM, JavierQQ wrote:
> On 4 nov, 11:42, Frederick Cheung <[email protected]> wrote:
>> 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

> Because as an example
> I have
> Evaluation 1
> - Question 1
> - Question 2
> 
> If I edit question 2 and change the number it will allow me to do that
> and it will show this
> Evaluation 1
> - Question 1
> - Question 1
> 
> And that's what I want to avoid
> 
> I tried  validates_uniqueness_of :qnumber, :scope
> => :evaluation_id, :on => :create
> and it works, but when I edit, I can do what I mention
----
what is the field/column that is 'Question 1'?  Is it one of the fields that 
you only validate on 'create'? You should probably remove ', :on => :create' 
because that won't protect uniqueness on 'update'

Craig

-- 
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