2016-08-22 238 views
0

我有两个模型,一个是customer.rb,第二个是money.rb。 关系是客户has_many:金钱belongs_to客户。我正在使用MySql远程数据库。当我尝试在同一时间,它只是正常工作来提取单个表中的数据,但问题是,当我尝试这样做:Ruby on Rails ActiveRecord :: StatementInvalid在客户中#show

<%= @customer.money.each do |m| %> 
    <%= m.id %> 
    <%= m.cid %> 
<% end %> 

它抛出一个错误的:mysql ::错误:未知列“money_tb。 customer_id'in'where clause':SELECT money_tb。* FROM money_tb where money_tbcustomer_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”

回答

0

如果您有不同的f oreign_key名称比“association_name_id”那么你需要通过:foreign_key选项belongs_tohas_many

class Customer < ActiveRecord::Base 
    has_many :money, foreign_key: "cid" 
end 

class Money < ActiveRecord::Base 
    belongs_to :customer, foreign_key: "cid" 
end 
+0

非常感谢#Michal_Mlozinak。它的工作,谢谢你.... –