Topic: Issue with many-to-many form error messages

I've constructed an association between an Affiliate and my Address model, as an Affiliate can have many addresses. An affiliate is a company and can have multiple offices, I need to record all addresses as they are later on used for geolocation.

Here is how I set up my associations:

class Affiliate < ActiveRecord::Base
  has_many :addresses, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for :addresses
  attr_accessible :addresses_attributes

class Address < ActiveRecord::Base
  attr_accessible :city, :state, :street_line_1, :street_line_2, :zip
  belongs_to :addressable, :polymorphic => true  
  validates_presence_of :street_line_1, :city, :state, :zip  

When an affiliate signs up (*affiliate#new*), I build an initial address association to populate the sign up form with at least one address.

class AffiliatesController < ApplicationController
  def new
    @affiliate =

Then, the address#new page form has the following:

%h3 How do we reach you?        
= f.simple_fields_for :addresses do |address|
  = address.input :street_line_1
  = address.input :street_line_2
  = address.input :city, :wrapper => false, :input_html => { :class => "city" }
  = address.input :state, :wrapper => false, :input_html => { :class => "state" }
  = address.input :zip, :wrapper => false, :as => :string, :input_html => { :class => "zip" }

This works wonderfully well, except for one problem. If the address validation fails, I get the following error messages

- Addresses street line 1 can't be blank
- Addresses city can't be blank
- Addresses state can't be blank
- Addresses zip can't be blank

As you can notice, Rails is using the plural for address as the association is many-to-many, but the validations that are failing are on an individual Address model, not the association!

Any ideas?

Thanks in advance.

Re: Issue with many-to-many form error messages

You can customise your error messages … sages-html

Nice job on getting the associations set up properly smile

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)