Hello,

I suppose this topic has been widely discussed, but i've not found
exactly what i need.

I thought validate_associations would in fact validate the associations
of my models, but it simply verifies that the fields of the object are
valid. So i need to create some custom validations to do

I found one solution:

[code]
validate :obj_must_exist

def obj_must_exist
  errors.add(:obj_id, "must point to an existing obj") if obj_id &&
obj.nil?
end
[/code]

But now i need it in a helper or something like that, because i've to
reuse the same validation several times. I created a file in
config/initializers called validations.rb and put this:

ActiveRecord::Base.class_eval do
  def self.validates_object_exists(obj, obj_id)
    obj.errors.add(obj_id, "must point to an existing #{obj}") if obj_id
|| obj.nil?
  end
end

But it's just not working... So, what's the "rails way" to do that?

Thank you.
-- 
Posted via http://www.ruby-forum.com/.

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