title: Nested fields and strong parameters
body: <p>I am having a complex issue in Rails 4, which I will try to describe below. I am using simple form and awesome_nested_fields gems.</p>
  
  <p>I have a bunch of <strong>events</strong> with some fields in my app</p>
  
  <p>Here's my working event_controller params before the implementation of accepts_nested_attributes:</p>
  
  <pre><code>private
  
    def event_params
      params.require(:event).permit(:title, :description, :type_id, :price, :program,
                                    :start_date, :end_date, :image, category_ids: [])
    end
  </code></pre>
  
  <p>Now I would like to add some speakers to my events, and make the user decide, how many speakers he wants per event. So I'm adding the nested fields gem and making speakers a nested field, as per their documentation.</p>
  
  <p>Event.rb</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
    has_many :speakers
    accepts_nested_attributes_for :speakers, allow_destroy: true
  end
  </code></pre>
  
  <p>Speaker.rb</p>
  
  <pre><code>class Speaker &lt; ActiveRecord::Base
    belongs_to :event
  end
  </code></pre>
  
  <p>Adding speakers (inside my add event simple_form_for):</p>
  
  <pre><code>  &lt;%= f.nested_fields_for :speakers do |f| %&gt;
        &lt;fieldset class="item"&gt;
          &lt;%= f.label :name %&gt;
          &lt;%= f.text_field :name %&gt;
  
          &lt;a href="#" class="remove"&gt;remove&lt;/a&gt;
  
          &lt;%= f.hidden_field :id %&gt;
          &lt;%= f.hidden_field :_destroy %&gt;
      &lt;/fieldset&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>Update controller for strong parameters:</p>
  
  <pre><code>private
  
    def event_params
      params.require(:event).permit(:title, :description, :type_id, :price, :program,
                                    :start_date, :end_date, :image, category_ids: [],
                                    speakers_attributes: [ :name ])
    end
  </code></pre>
  
  <p>Now when I launch my app, upon creation of the new event I'm getting:</p>
  
  <pre><code>can't write unknown attribute `event_id'
  </code></pre>
  
  <p>If I remove the </p>
  
  <pre><code>speakers_attributes: [ :name ]
  </code></pre>
  
  <p>from the strong parameters I will be able to create my event, however when trying to view or edit it I will get </p>
  
  <pre><code>SQLite3::SQLException: no such column: speakers.event_id: SELECT "speakers".* FROM "speakers"  WHERE "speakers"."event_id" = ?
  </code></pre>
  
  <p>And of course there are no speakers created in the database.</p>
  
  <pre><code>&gt;&gt; s = Speaker.first
  =&gt; nil
  </code></pre>
  
  <p>I will appreciate any help or advice. Thank you!</p>
  
  <p>=====</p>
  
  <p>UPDATED for the duplications issue</p>
  
  <p>Events controller</p>
  
  <pre><code>  def update
      @event = Event.find(params[:id])
  
      if @event.update(event_params)
        flash[:success] = "Event updated"
        redirect_to @event
      else
        render @event  
      end
    end
  </code></pre>
  
  <p>event.rb</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
  
    belongs_to :type
  
    has_many :categorizations
    has_many :categories, through: :categorizations
    has_many :speakers
    accepts_nested_attributes_for :speakers, allow_destroy: true
  
    @today = Date.today
  
    def self.future_events
      order('start_date ASC').where('start_date &gt;= ?', @today)
    end
  
    scope :current_events,  lambda { where('start_date &lt; ? and end_date &gt; ?', @today, @today) }
    scope :past_events,     lambda { order('end_date DESC').where('end_date &lt; ?', @today) }
  
    scope :future_by_type,   lambda { |type| order('start_date ASC').where('type_id = ? and start_date &gt;= ?', type, @today) }
    scope :current_by_type,  lambda { |type| order('start_date ASC').where('type_id = ? and start_date &lt; ? and end_date &gt; ?', type, @today, @today) }
    scope :past_by_type,     lambda { |type| order('start_date ASC').where('type_id = ? and end_date &lt; ?', type, @today) }
  
    validates :title, presence: true
  
    mount_uploader :image, ImageUploader
  
    before_save :define_end_date
    before_save :zero_price
  
      private
  
        def define_end_date
          self.end_date ||= self.start_date
        end
  
        def zero_price
          if self.price.empty?
            self.price = 0
          end 
        end
  end
  </code></pre>