0

I have a nested form inside a parent form that allows you to add fields inside the parent form. The idea is that a book can have many schools and the school belongs to the book(habtm) relationship. here are my models:

books.rb

accepts_nested_attributes_for :author
accepts_nested_attributes_for :gallery, :allow_destroy => true
accepts_nested_attributes_for :schools, :reject_if => :find_school, :allow_destroy => true
accepts_nested_attributes_for :images, :allow_destroy => true


def find_school(school)
    if existing_school = School.find_by_school_name(school['school_name'])
       self.schools << existing_school
      return true
    else
      return false
    end
end

school.rb

class School < ActiveRecord::Base

    has_and_belongs_to_many :books

    validates :school_name, :address, presence: true
    #validates_uniqueness_of :school_name
end

In my form for the book I have this setup:

<div id="school" class="field">
      <h3>Schools reading this book (add the name and full address of the school)</h3>
      <%= f.simple_fields_for :schools, :wrapper => 'inline' do |builder| %>  
        <%= render 'school_fields', :f => builder %>
        <%= link_to_add_association 'add school', f, :schools, :render_options => {:wrapper => 'inline' }, :class => 'fa fa-plus' %>
      <% end %>
    </div>

In here it should render the fields but I get this

the fields in the edit form after update

The issue i have is that in my book.rb model the find_school method keeps returning multiple values of the same books when I update the book. The idea is that the find_school method basically stops duplicate schools being created as records and just assigns them as a relationship to the book instead. What I am finding is that this self.schools << existing_school on update just keeps duplicating the fields in the edit form and adds the fields as entries on the item when I check in the params output.

Can anyone help with this

M dunbavan
  • 1,157
  • 5
  • 22
  • 57
  • Have you considered using a scoped validation, like [this](http://stackoverflow.com/a/8247971/382982)? – pdoherty926 Oct 10 '14 at 12:55
  • 1
    Your title suggests that updating is causing duplicate entries to be created, but you describe trying to prevent duplicates from being created using find_school, which is it? – Ian M Oct 10 '14 at 12:57
  • Also, have you tried a `puts School.find_by_school_name(school['school_name'])` to see if it is returning correctly? I suspect you need something more like `School.find_by_school_name(school[school.name])` – Ian M Oct 10 '14 at 13:00
  • I tried those above but nothing happened. The issue is stopping duplicate entries being created by find_school – M dunbavan Oct 10 '14 at 13:21
  • Where is the code that saves the book? Where is the code that calls `find_school`. To me, it seems like the problem is this. 1) Book has no schools. 2) Add school to book, press save. 3) `find_school` is called and adds schools to the book's school list. 4) The book's school list already contained all of its schools, so the list is duplicated. – Max Oct 10 '14 at 14:35
  • Max I think you hit the nail on the head there as that is whats happenin but i am not sure how to fix it? – M dunbavan Oct 10 '14 at 15:16

0 Answers0