Rails 4 - 由于:has_many关联而无法修改关联

时间:2014-06-25 18:37:34

标签: ruby-on-rails

当我尝试提交嵌套表单时,我收到以下错误。

Cannot modify association 'Appointment#addresses' because the source reflection class 'Address' is associated to 'User' via :has_many

我不完全确定我的设置的哪一部分是错误的。简要说明一下,我的用户有多个约会和多个地址。每个约会都可以在不同的地址发生,这就是为什么我通过用户进行:has_many关联(这是正确的,对吧?)。为什么我会收到此错误?

以下是我的模特:

class User < ActiveRecord::Base
  has_many :addresses, dependent: :destroy
  has_many :appointments, dependent: :destroy
end

class Appointment < ActiveRecord::Base
  belongs_to :user
  has_many :addresses, :through => :user
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :user
end

这是我的Appointments控制器中的create方法:

class AppointmentsController < ApplicationController

  ...

  def create
    @appointment = current_user.appointments.build(appointment_params)
    @address = @appointment.addresses.build(appointment_params[:addresses_attributes]["0"])

    respond_to do |format|
      if @appointment.save
        format.html { redirect_to current_user, notice: 'Appointment was successfully created.' }
        format.json { render :show, status: :created, location: current_user }
      else
        format.html { render :new }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  ...

  private
    def appointment_params
      params.require(:appointment).permit(:appointment_date, :appointment_start_time, :appointment_end_time, :comments, :phone_number, addresses_attributes: [:user_id, :street_address, :street_address_optional, :city, :state, :zip_code, :primary])
    end

end

最后,在我看来,这是我的表格:

<%= form_for(@appointment, :url => {:controller => "appointments", :action => "create"}, :html => {"data-abide" => ""}) do |f| %>
      <label>
        Appointment Date
      </label>
      <%= f.date_select :appointment_date %>

      <label>
        Appointment Timeframe Start
      </label>
      <%= f.time_select :appointment_start_time %>

      <label>
        Appointment Timeframe End
      </label>
      <%= f.time_select :appointment_end_time %>

  <%= f.fields_for :addresses do |builder| %>
    <%= builder.hidden_field :user_id, :value => current_user.id %>

        <label>
          Street Address
          <%= builder.text_field :street_address %>
        </label>

        <label>
          Street Address (Optional)
          <%= builder.text_field :street_address_optional %>
        </label>

        <label>
          City
          <%= builder.text_field :city %>
        </label>

        <label>
          State
          <%= builder.text_field :state %>
        </label>

        <label>
          Zip Code
          <%= builder.number_field :zip_code %>
        </label>

        <%= builder.check_box :primary %><%= builder.label :primary %>

  <% end %>

      <label>
        Special Instructions
        <%= f.text_area :comments %>
      </label>

      <%= f.submit "Sign Up", :class => "button expand"%>

<% end %>

提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:4)

用户可以有多个约会,但每个约会都在一个地址中。 (除非他可以自己多位置)。

所以你应该这样做:

class User
  has_many :appointments

class Appointment
  has_one :address

class Address
  belongs_to :appointments

如果要检索用户必须执行约会的地址:

@addresses = current_user.appointments.map {|app| app.address}