[英]Ruby on Rails ActiveRecord::StatementInvalid in Customers#show
我有兩個模型,一個是customer.rb,第二個是money.rb。 關系是客戶has_many:money,而錢屬於客戶。 我正在使用MySql遠程數據庫。 當我一次嘗試從單個表中提取數據時,它可以很好地工作,但是問題是當我嘗試這樣做時:
<%= @customer.money.each do |m| %>
<%= m.id %>
<%= m.cid %>
<% end %>
它引發錯誤:Mysql :: Error:“ where子句”中的未知列“ money_tb.customer_id”:SELECT money_tb
。* FROM money_tb
WHERE money_tb
。 customer_id
=? 這是我的show.html.erb的片段:
<h1><%= @customer.name %></h1>
<%= @customer.money.each do |m| %>
<%= @m.id %>
<%= @m.cid %>
<%= @m.customer.name %>
<% end %>
這是我的customers_controller:
class CustomersController < ApplicationController
def index
@customers = Customer.all
end
def new
@customer = Customer.new
end
def create
@customer = Customer.new(customer_params)
if @customer.save
flash[:notice] = "Customer Create"
redirect_to new_customer_path
else
render 'new'
end
end
def edit
@customer = Customer.find(params[:id])
end
def update
@customer = Customer.find(params[:id])
if @customer.update(customer_params)
flash[:notice] = "Customer Updated"
redirect_to customers_path
else
render 'edit'
end
end
def show
@customer = Customer.find(params[:id])
end
private
def customer_params
params.require(:customer).permit(:name, :address, :ph_no)
end
end
這是模型:
class Customer < ActiveRecord::Base
self.table_name = "cust_tb"
has_many :money
has_many :gold
end
我也要提到,錢表中的customer_id字段表示為“ cid”
如果您具有與“ association_name_id”不同的外鍵名稱,則需要將:foreign_key
選項傳遞給belongs_to
和has_many
。
class Customer < ActiveRecord::Base
has_many :money, foreign_key: "cid"
end
class Money < ActiveRecord::Base
belongs_to :customer, foreign_key: "cid"
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.