class Person < ActiveRecord::Base has_many :addresses has_one :primary_address, :class_name => 'Address', :order => 'preferred desc, id'
accepts_nested_attributes_for :addresses, :allow_destroy => true accepts_nested_attributes_for :primary_address, :allow_destroy => true end
class Address < ActiveRecord::Base belongs_to :person end
In the view: <% f.fields_for :primary_address do |address_builder| %> <%= render 'addresses', :f => address_builder %> <% end %>
The 'addresses' table has a boolean column named 'preferred' and there should be only one address for the person that is the preferred one.
Testing on the console the 'has_one' definition gives me the 'preferred' address if there is one, otherwise it gives me the first one it finds or returns nil if there are no addresses for the person. All this is working as expected.
If my association has 2 addresses (I'm making sure one of them is 'preferred' and the other one is not), when the view code runs it does not output just one address (which is what I need), it outputs 2 addresses, actually the same (preferred) address twice.
Is there a way to make fields_for do what I need it to do?
Thanks in advance.