:has_many :through form validation messages problem

kindly help with this Im stuck

below are the models

class Listing < ActiveRecord::Base   has_one :template   has_many :lvalues, :dependent=>:delete_all   has_many :custom_fields, :through => :lvalues   has_and_belongs_to_many :categories   belongs_to :user   cattr_reader :per_page   validates_presence_of :name, :message=>"^Listing title is required"   @@per_page = ConfigListing.find(:first).listings_per_page

end

class Lvalue < ActiveRecord::Base belongs_to :custom_field   belongs_to :listing # validates_presence_of :value   has_one :file_or_image, :dependent=>:delete   has_attachment :content_type => :image,                  :storage => :file_system,                  :max_size => 500.kilobytes,                  :resize_to => '320x200>',                  :thumbnails => { :thumb => '100x100>' }

# validates_as_attachment #validates_presence_of :value, :if=>:is_required?     def is_required?       custom_field.required?     end

def validate    errors.add_to_base "#{custom_field.name} is required" if value.blank? and is_required? end end

im showing errors like this <%= error_messages_for(:object=>[@listing.lvalues, @listing]) %>

below is the result for the validation messages

Select Date Time is required date is required Listing title is required Lvalues is invalid Lvalues is invalid

first three lines are ok but I dont want the default messages for every child validation failure. how is it possible i dont want "Lvalues is invalid" because i have multiple Lvalues in one form and its quite confusing for me.

Usman Akram wrote:

below is the result for the validation messages

Select Date Time is required date is required Listing title is required Lvalues is invalid Lvalues is invalid

first three lines are ok but I dont want the default messages for every child validation failure. how is it possible i dont want "Lvalues is invalid" because i have multiple Lvalues in one form and its quite confusing for me.

I was wondering the same thing...Have you found a solution?

I was wondering the same thing...Have you found a solution?

No man just waiting for some help. its not a bug the validation takes place only once I just dont know how to switch off the invalid record notification. u can see the custom validation messages are being sent out but also along with the invalid child validation . if you find something kindly inform Regards! Usman

I was wondering the same thing…Have you found a solution?

No man just waiting for some help. its not a bug the validation takes place only once I just dont know how to switch off the invalid record notification. u can see the custom validation messages are being sent

out but also along with the invalid child validation . if you find something kindly inform Regards! Usman

– Posted via http://www.ruby-forum.com/.

I had to come up with a hack to get around this (duplicate and generic validation error messages). I also wanted to include my specific error messages from the child models. Basically, this is my custom validator:

  #custom validator so i can make error messages appear the way i want   #(instead of validates_associated)   def valid_associated_records?     associated_records.each do |ar|       if(!ar.nil? && !ar.valid?)         #get rid of the default error message         errors.delete(:attribute_name)

        #take specific error messages from children, put them in this model's errors         ar.errors.each do |attr, msg|           errors.add(attr, msg)         end       end     end   end

The tricky part is errors.delete--the errors class doesn't expose a delete method, even though it uses a hash as an underlying data type, which does have a delete method. So I created a file (lib/ error_delete.rb) with this in it to extend the Errors class and expose it:

module ActiveRecord   class Errors     # add function to delete a particular error message     def delete(key)       @errors.delete(key.to_s)     end   end end

Put in "require 'error_delete'" at the top of the file where you call the delete function. Hope that's helpful!

thanx will check this tonight

I took a similar approach, not sure if it's the best way. I added the delete method like you did for class Errors, then in my top level model (in my case it was a "Match" and the child models were "PlayerGame" objects ):

def after_validation     self.errors.delete(:player_games)     player_games.each_with_index do |game, i|       game.validate       game.errors.each_full do |msg|         self.errors.add_to_base "Player game record #{i+1} #{msg}"       end     end   end

It's probably not the most performant way to do things but it works well.

I was just wondering where the "associated_records" comes from? Just tried your idea but got a:

   "undefined local variable or method `associated_records'"

Where did you put "valid_associated_records?" exactly? Is it just a method you call from within the model's "validate" function?

thanks