Forum: Ruby on Rails validation on before_add callback

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-05 16:30
problem: validation error is not functionning in callback

callback for many-to-many association:

# keyword.rb
:before_add => :reject_self_related


    def reject_self_related(related_keyword)
        if (related_keyword == self)
# this does not work:
            errors.add(:name, "Relating a keyword to itself is
forbidden!")
            raise "Relating a keyword to itself is forbidden!"
        end
    end


in the keyword.rhtml form:

<%= error_messages_for 'keyword' %>

does not display the error message.

what am I doing wrong?
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-05 16:46
Isabelle wrote:

Hi Isabelle,

First off, is the callback being triggered at all ?  If you replace the
whole body with the raise, do you see the exception ?

Second, if you just replace the body with the errors.add, do you see the
error ?

Last, == on Object tests for the exact same Object, I think ActiveRecord
override this to compare ids, if the new item hasn't yet been saved, the
== method will return false.  Perhaps you need to check, say
keyword.text, or whatever the property is that stores the text.

Alan
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-05 17:15
Alan Francis wrote:
> Isabelle wrote:
>
> Hi Isabelle,
>
> First off, is the callback being triggered at all ?  If you replace the
> whole body with the raise, do you see the exception ?

yes. I can catch the exception in the controller and display the
exception message:

# keywords_controller.rb

        # attempt to add item to list
        begin
          @keyword.related_keywords<< @related
        rescue Exception
          # extract error messages from the global variable
          # representing the Exception object
          flash[:notice] = $!.message
        end

> Second, if you just replace the body with the errors.add, do you see the
> error ?

no, and the object that should not have been saved is saved.

> Last, == on Object tests for the exact same Object, I think ActiveRecord
> override this to compare ids, if the new item hasn't yet been saved, the
> == method will return false.  Perhaps you need to check, say
> keyword.text, or whatever the property is that stores the text.

I see your point. In my case the callback is before adding an existing
item to a collection that is defined as a self-referential
has_and_belongs_to_many relation.
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-05 17:21
> I see your point. In my case the callback is before adding an existing
> item to a collection that is defined as a self-referential
> has_and_belongs_to_many relation.

I'd reckon that's the problem then, and would compare the attribute,
rather than the object.
Cac068c9c92719a59db960b2fa51719c?d=identicon&s=25 Isabelle (Guest)
on 2006-04-06 16:34
Alan Francis wrote:
>
>> I see your point. In my case the callback is before adding an existing
>> item to a collection that is defined as a self-referential
>> has_and_belongs_to_many relation.
>
> I'd reckon that's the problem then, and would compare the attribute,
> rather than the object.

tried it and still the errors.add() is not executed
This topic is locked and can not be replied to.