我有一个具有当前位置字段(城市和国家/地区)的用户实体。为了保存这些信息,我创建了一个名为 Location 的实体,它有很多用户。
我不完全确定是否应该放入用户模型“has_one”或“belongs_to”,但根据我读到的内容,如果我希望它具有位置的外键,我应该放入“belongs_to”。我还希望能够在编辑用户时编辑用户的当前位置。所以我使用嵌套属性。但是,当我编辑用户时,我最终每次都会添加一个新位置,而不会将其与编辑的用户相关联。你能帮我吗?
我的代码如下:
#User Model
class User < ActiveRecord::Base
## Relationships
belongs_to :current_location, :class_name => 'Location'
accepts_nested_attributes_for :current_location
end
#Location Model
class Location < ActiveRecord::Base
#Relationship
has_many :users
end
# part of the _form_edit.haml
- form_edit.fields_for :current_location do |location_form|
= location_form.label :location, "Current Location"
= location_form.text_field :location
#Application Helper
#nested attributes for user and location
def setup_user(user)
returning(user) do |u|
u.build_current_location if u.current_location.nil?
end
end
#in the user controller (added after edit)
def update
@user = @current_user
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
正如其他人指出的那样,您面临的确切问题是您的控制器没有收到应有的位置 ID。在我看来,位置 ID 是通过错误的参数传递的。不幸的是,新记录中不存在位置 ID,因此在表单中这是不可能的。
您的问题源于对belongs_to关系使用accepts_nested_attributes_for。该行为没有明确定义。这似乎是一个已记录的错误。因此,accepts_nested_attributes_for 应该位于关系的一个或多个方面。
以下是一些可能的解决方案:
-
将 Accepts_nested_attributes_for 移至 Location 模型并以相反的方式构建表单。
-form_for @location do |location_form|
...
=location_form.fields_for @user do |user_form|
....
不幸的是,这不允许以逻辑方式呈现信息。并且使得编辑正确的用户变得困难。
-
使用连接模型,并建立一个具有一:通过关系。
老实说,我不确定 Accept_nested_attributes_for 与 :through 关系的配合效果如何,但它肯定会解决您链接记录的问题。
-
忽略 Accept_nested_attributes_for 并以老式方式处理控制器中的关联。
实际上保留accepts_nested_attributes_for。它提供了一些方便的方法,只是不要让它到达 update_attributes/create 语句。
def update
@user = @current_user
completed = false
location_params = params[:user].delete(:current_location_attributes)
User.transaction do
@location = Location.find_or_create_by_id(location_params)
@user.update_attributes(params[:user])
@user.current_location = @location
@user.save!
completed = true
end
if completed
flash[:notice] = "Account updated!" redirect_to account_url
else
render :action => :edit
end
end
如果不创建新位置,则 的字段将自动填充 current_location_attributes 哈希中的 id 字段。但是,find_or_create_by_id 需要哈希中的 :id 条目才能工作。如果 id 不在数据库中,它将使用正确的自动递增 id 创建。如果您要创建新位置,则需要添加它。最简单的方法是将其添加到表单中=location_form.hidden_field :id, 0 unless current\_location.new\_record?
.
但是,您可能希望减少重复的位置创建,并将 Location.find_or_create_by_id 行更改为 Location.find_or_create_by_location。这也将减少因唯一性验证失败而产生的任何错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)