在Rails中,如何在新对象的Create方法中自动更新用户以拥有另一个类的新创建对象?

我的应用程序有UsersDwellings 。 当user创建dwellinguser通过铁轨协会成为dwellingowner 。 通过我的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的定义是什么,我们不知道您的用户是如何创建的,我们不知道有关您的表单的任何细节等。

我现在没有太多时间研究这个问题,但你可能会尝试以下方法。

  1. 首先,从您的User模型中删除has_secure_password ,看看您是否仍然遇到此问题。 这仅用于故障排除,因为最终您需要重新添加has_secure_password

  2. 确保您的users表具有password_digest列。

  3. 确保current_userhas_secure_password的users表中有一个值。也许您在添加has_secure_passwordfunction之前创建了用户,这将导致password_digest的NULL值。

  4. 确保current_user实际上是持久化(即在数据库中)用户。 您可以通过在current_user.save!添加以下行来完成此操作current_user.save!

     raise StandardError, "current_user is not persisted" unless current_user.persisted? 
  5. 我假设current_user已经作为持久用户存在。 如果您实际上正在尝试以同样的forms创建用户和住所,那么还有其他几个因素需要考虑。

我暂时没有时间来帮助解决这个问题。 祝愿它得到解决。

这是保存user的问题。 我在Dwellings控制器中更新了我的create操作,以使用user.save!强制保存user user.save! 打破应用程序并获得明确的错误。 Rails抱怨缺少password (和密码长度)和password_confirmation 。 我正在对passwordpassword_confirmation强制执行presencevalidation,并在我的User模型中对password进行lengthvalidation。 我更新了这些仅在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 }