[英]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.