0

我的应用程序有UsersDwellings。当 auser创建 adwelling时,user成为through a rails 关联的ownerdwelling通过我的create方法,user_id已成功分配给列中新创建dwellingowner_id,但是dwelling_id没有传播到dwelling_iduser记录中。我不确定是否是关系或方法设置不正确,但是当尝试保存 时似乎会出现问题user,因为dwelling已成功创建。以下是我的模型和方法实现。

住宅模型

# dwelling.rb
class Dwelling < ActiveRecord::Base
  attr_accessible :street_address, :city, :state, :zip, :nickname

  belongs_to :owner, :class_name => "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 < ActiveRecord::Base
  attr_accessible :email, :first_name, :last_name, :password, :password_confirmation, :zip
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  belongs_to :dwelling
  has_many :properties, :class_name => "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条件触发了“...但有些东西阻止我们将您添加为新秀...”闪光灯。

更新 2012 年 8 月 3 日晚上 9:11 东部标准时间

我更新了create用于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
4

3 回答 3

1

更新:为了清楚起见,我误解了下面答案中的用户/居住关系,所以这不是问题的解决方案。


我认为您误解了关联的工作方式。使用belongs_to/has_many关系,只有属于的模型(在本例中为Dwelling)具有指向另一条记录的外键(在本例中,User通过外键owner_id)。另一条记录 ( User) 不需要每个关联的键,这将是多余的。

同样,您不需要分配@dwellingcurrent_user.dwelling,也不需要保存current_user. 只需@dwelling在构建后保存就current_user.properties.build(params[:dwelling])足够了。

于 2012-08-05T00:35:40.763 回答
0

确切的问题实际上取决于我们尚未看到的应用程序的详细信息。例如,我们不知道current_user定义是什么,我们不知道您的用户是如何创建的,我们不知道有关您的表单的任何详细信息等。

我现在没有更多的时间来处理这个问题,但是您可以尝试以下操作。

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

  2. 确保您的用户表有一个password_digest列。

  3. 确保current_user在 users 表中有一个值password_digest 也许您在添加has_secure_password功能之前创建了用户,这将导致 NULL 值password_digest

  4. 确保它current_user实际上是一个持久化(即在数据库中)用户。您可以通过在上面添加以下行来做到这一点current_user.save!

    raise StandardError, "current_user is not persisted" unless current_user.persisted?
    
  5. 我假设current_user已经作为持久用户存在。如果您实际上是在做一些更奇特的事情,例如尝试以相同的形式创建用户和住宅,那么还需要考虑其他几个因素。

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

于 2012-08-05T03:22:45.600 回答
0

这是保存user. 我更新createDwellings控制器中的操作以强制保存user使用user.save!来破坏应用程序并获得明确的错误。Rails 抱怨缺少password(和密码长度)和password_confirmation. 我正在对我的模型进行presence验证passwordpassword_confirmation以及lengthpassword我的User模型进行验证。我将这些验证更新为仅在create. 这解决了这个问题。由于不再强制执行验证user.savedwelling_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 }
于 2012-08-06T15:03:35.293 回答