简体   繁体   English

Rails 4-对嵌套多态属性进行更新操作的多态关联

[英]Rails 4 - Polymorphic associations with update action on nested polymorphic attributes

I'm trying to figure out a problem I seem to keep having in setting up polymorphic associations in my Rails 4 app. 我正在尝试找出在Rails 4应用程序中设置多态关联时似乎一直遇到的问题。

I have a project model and an address model. 我有一个项目模型和一个地址模型。 The associations are: 关联是:

Profile 轮廓

has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

Address 地址

belongs_to :addressable, :polymorphic => true

I previously asked this question on the same problem. 我之前曾在同一问题上问过这个问题。 I couldn't (and still can't) understand the answers in that post: Rails 4 - Polymorphic associations 我无法(仍然无法)理解该帖子中的答案: Rails 4-多态关联

This time around - I'm having a problem that is triggered when I try to update a profile by inserting an address. 这次-我遇到一个问题,当我尝试通过插入地址来更新配置文件时触发。 The error message identifies the problem as coming from the update action in the profiles controller. 该错误消息将问题标识为来自配置文件控制器中的更新操作。 The update action has: 更新操作具有:

My profiles controller update action has: 我的个人档案控制器更新操作包括:

def update

    # successful = @profile.update(profile_params)

    # Rails.logger.info "xxxxxxxxxxxxx"
    # Rails.logger.info successful.inspect
    # user=@profile.user
    # user.update.avatar
    # Rails.logger.info "prof xxxxxxxxxxxxx"
    # Rails.logger.info @profile.update(profile_params)
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

The error message says: 错误消息显示:

ERROR:  duplicate key value violates unique constraint "index_addresses_on_addressable_type_and_addressable_id"
DETAIL:  Key (addressable_type, addressable_id)=(Profile, 1) already exists.

Does anyone know what this message means, and how to address it? 有谁知道此消息的含义以及如何解决?

In your database, you have set a unique constraint: , you can go to the database to see what you have set by the name of "index_addresses_on_addressable_type_and_addressable_id". 在数据库中,您设置了一个唯一约束:,您可以转到数据库以查看名称为“ index_addresses_on_addressable_type_and_addressable_id”的设置。 As the error message show, you try to update a record with value ( Profile , 1) which has already been used by another record. 如错误消息所示,您尝试使用值(Profile,1)更新一条记录,该记录已被另一条记录使用。
To solve this issue, there are two solutions: one is from the database side: you need to know why there is a unique constraint about addresses. 要解决此问题,有两种解决方案:一种是从数据库端解决的:您需要知道为什么地址存在唯一约束。 if it is not need , you can remove it from the database. 如果不需要,可以将其从数据库中删除。 the other is ensure the (addressable_type, addressable_id) is unique before you update your data into database. 另一个是在将数据更新到数据库之前,确保(addressable_type,addressable_id)是唯一的。

hope this can give a kind of help 希望这可以提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM