在Rails中,如何在新对象的Create方法中自动更新用户以拥有另一个类的新创建对象?
我的应用程序有Users
和Dwellings
。 当user
创建dwelling
, user
通过铁轨协会成为dwelling
的owner
。 通过我的create
方法, user_id
被成功分配给owner_id
列中新创建的dwelling
,但是dwelling_id
不会传播到user
记录中的dwelling_id
。 我不确定是否是错误设置的关系或方法,但是当成功创建dwelling
时,尝试保存user
时似乎会出现问题。 以下是我的模型和方法实现。
住宅模型
# dwelling.rb class Dwelling "User", :foreign_key => "owner_id" has_many :roomies, :class_name => "User" validates :street_address, presence: true validates :city, presence: true validates :state, presence: true validates :zip, presence: true end
–
用户模型
# user.rb class User "Dwelling", :foreign_key => "owner_id" ...
–
住宅控制器(创建方法)
# dwellings_controller.rb def create @dwelling = current_user.properties.build(params[:dwelling]) if @dwelling.save current_user.dwelling = @dwelling if current_user.save flash[:success] = "Woohoo! Your dwelling has been created. Welcome home!" else flash[:notice] = "You have successfully created a dwelling, but something prevented us from adding you as a roomie. Please email support so we can try to correct this for you." end redirect_to current_user else render 'new' end end
–
如上所述, dwelling
已成功创建,但“……但有些东西阻止我们将你添加为新手……”闪光是从else
条件触发的。
更新8/3/12美国东部时间下午9:11
我更新了create
action以使用user.save!
强迫交易。 输出以下错误。 显然,密码是以某种方式需要的。
ActiveRecord::RecordInvalid in DwellingsController#create Validation failed: Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
更新:为了清楚起见,我在下面的答案中误解了用户/居住关系,所以这不是问题的解决方案。
我认为你误解了协会的运作方式。 对于belongs_to
/ has_many
关系,只有属于 (在这种情况下, Dwelling
)的模型具有指向另一个记录的外键(在这种情况下, User
通过外键owner_id
)。 另一条记录( User
)不需要每个关联的密钥,这将是多余的。
同样,您不需要将@dwelling
分配给current_user.dwelling
,也不需要保存current_user
。 在current_user.properties.build(params[:dwelling])
构建它之后保存@dwelling
就足够了。
确切的问题实际上取决于我们尚未见过的应用程序的详细信息。 例如,我们不知道current_user
的定义是什么,我们不知道您的用户是如何创建的,我们不知道有关您的表单的任何细节等。
我现在没有太多时间研究这个问题,但你可能会尝试以下方法。
-
首先,从您的
User
模型中删除has_secure_password
,看看您是否仍然遇到此问题。 这仅用于故障排除,因为最终您需要重新添加has_secure_password
。 -
确保您的users表具有
password_digest
列。 -
确保
current_user
在has_secure_password
的users表中有一个值。也许您在添加has_secure_password
function之前创建了用户,这将导致password_digest
的NULL值。 -
确保
current_user
实际上是持久化(即在数据库中)用户。 您可以通过在current_user.save!
添加以下行来完成此操作current_user.save!
:raise StandardError, "current_user is not persisted" unless current_user.persisted?
-
我假设
current_user
已经作为持久用户存在。 如果您实际上正在尝试以同样的forms创建用户和住所,那么还有其他几个因素需要考虑。
我暂时没有时间来帮助解决这个问题。 祝愿它得到解决。
这是保存user
的问题。 我在Dwellings
控制器中更新了我的create
操作,以使用user.save!
强制保存user
user.save!
打破应用程序并获得明确的错误。 Rails抱怨缺少password
(和密码长度)和password_confirmation
。 我正在对password
和password_confirmation
强制执行presence
validation,并在我的User
模型中对password
进行length
validation。 我更新了这些仅在create
上强制执行的validation。 这解决了这个问题。 在user.save
上不再强制执行validation后, dwelling_id
已成功添加到user
记录中。 新实现的代码如下。
#dwellings_controller.rb ... def create @dwelling = current_user.properties.build(params[:dwelling]) if @dwelling.save current_user.dwelling = @dwelling if current_user.save flash[:success] = "Woohoo! Your dwelling has been created. Welcome home!" else flash[:notice] = "You have successfully created a dwelling, but something prevented us from adding you as a roomie. Please email support so we can try to correct this for you." end redirect_to current_user else render 'new' end end ...
–
#user.rb ... validates :password, presence: { :on => :create }, length: { minimum: 6, :on => :create } validates :password_confirmation, presence: { :on => :create }