2012-12-27 23 views
1

我在嘗試更新我的用戶表中的customer_id列時收到此錯誤,但找不到原因。用戶模型has_many:訂單和訂單模型belongs_to:user,訂單表具有users_id的正確外鍵以及依賴於此關係的其他事情正在正常工作。未定義的局部變量或方法`customer_id',但customer_id字段存在,並且存在一對多關聯

錯誤:

undefined local variable or method `customer_id' for #<Order:0x007ff27014f9f0> 

app/models/order.rb:16:in `save_with_payment' 
app/controllers/orders_controller.rb:53:in `block in create' 
app/controllers/orders_controller.rb:52:in `create' 

order.rb

belongs_to :user 

def save_with_payment 
if valid? 
    customer = Stripe::Customer.create(description: email, card: stripe_card_token) 
    self.stripe_customer_token = customer.id 
    self.user.update_column(customer_id, customer.id) # this is line 16 
    save! 

    Stripe::Charge.create(
     :amount => (total * 100).to_i, # in cents 
     :currency => "usd", 
     :customer => customer.id 
) 

end 
rescue Stripe::InvalidRequestError => e 
logger.error "Stripe error while creating customer: #{e.message}" 
errors.add :base, "There was a problem with your credit card." 
false 
end 

orders_controller.rb

def create 
if current_user 
    @order = current_user.orders.new(params[:order]) 
else 
    @order = Order.new(params[:order]) 
end 
respond_to do |format|  # this is line 52 
    if @order.save_with_payment # this is line 53 

    format.html { redirect_to auctions_path, :notice => 'Your payment has been successfully processed and your credit card has been linked to your account.' } 
    format.json { render json: @order, status: :created, location: @order } 
    format.js 
    else 
    format.html { render action: "new" } 
    format.json { render json: @order.errors, status: :unprocessable_entity } 
    format.js 
    end 
end 
end 

user.rb

has_many :orders 

回答

3
self.user.update_column(:customer_id, customer.id) 
+0

哦,你美!那個遺漏的結腸毀了我的聖誕節! – railsy

+0

聖誕快樂) –

相關問題