Rails .update失败 - id是未知属性?

时间:2014-07-28 22:16:49

标签: postgresql ruby-on-rails-4 sql-update

这个错误令我感到困惑。这是控制器代码(不是Estimates控制器)

...

@estimate = Estimate.find_by_link(params[:link])

...

if @estimate.customer_id.nil?
  @estimate.update_attributes(customer: current_user)
end

它给了我以下错误:

SQL (13.0ms)  UPDATE "estimates" SET "customer_id" = 24, "updated_at" = '2014-07-28 21:59:26.193000' WHERE "estimates"."id" = 277
Completed 500 Internal Server Error in 436ms

ActiveModel::MissingAttributeError (can't write unknown attribute 'estimate_id'):
 app/controllers/quotes_controller.rb:60:in 'new'

然而这很好用:

if @estimate.customer_id.nil?
  Estimate.find_by_link(params[:link]).update(customer: current_user)
end

如果我使用.update而不是.update_attributes

,它会做同样的事情

我正在运行Rails 4.0.0和Postgres

增加型号:

class Estimate < ActiveRecord::Base

 include DateTimeAttribute
 date_time_attribute :due_date

 has_many :translation_jobs, :dependent => :destroy
 has_many :price_adjustments, as: :adjustable
 has_one  :prospect
 has_one  :quote

 accepts_nested_attributes_for :translation_jobs, :allow_destroy => true

 belongs_to :customer, :foreign_key => :customer_id, :class_name => "User"
 belongs_to :prospect
 belongs_to :service

 attr_accessor :email, :opt_in

 validate :user_xor_email
 validates_associated :translation_jobs

 def email_valid
  self.email.match(/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i)
 end

 private

 def user_xor_email
  if customer_id.blank? and prospect_id.blank?
   if email.blank?
    errors.add(:email, "Please provide an email address")
   elsif !email_valid
    errors.add(:email, "Please provide a valid email address")
   end
  end
 end
end


class User < ActiveRecord::Base
 devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

 attr_accessor :login

 validates :username, :presence => true,
        :length   => { :maximum => 15 },
        :uniqueness => { :case_sensitive => false }

 validates :first_name, :presence => true,
        :length   => { :maximum => 30 }
 validates :last_name, :presence => true,
        :length   => { :maximum => 30 }

 has_many :companies
 has_many :user_account_indices
 has_many :accounts, :through => :user_account_indices
 has_many :buyers, :class_name => "User", :foreign_key => "manager_id"
 has_many :orders
 has_one :agent
 belongs_to :manager, :class_name => "User"

 accepts_nested_attributes_for :accounts

 before_destroy :check_dependencies

 ROLES = %w[admin manager agent client buyer guest]

 def role_symbols
  [role.to_sym]
 end

 def name
  self.first_name + ' ' + self.middle_name + ' ' + self.last_name
 end

 def active_for_authentication?
  super && !blocked?
 end

 def self.find_first_by_auth_conditions(warden_conditions)
  conditions = warden_conditions.dup
  if login = conditions.delete(:login)
   where(conditions).where(["lower(username) = :value OR lower(email) = :value", {:value => login.downcase }]).first
  else
   where(conditions).first
  end
 end

 def check_dependencies
  if self.companies.count > 0
   return false
  elsif self.accounts.count > 0
   return false
  elsif self.agent
   return false
  else
   return true
  end
 end
end

0 个答案:

没有答案