2016-08-22 2 views
0

私は2つのモデルがcustomer.rbであり、2つ目はmoney.rbです。 関係は、顧客has_many:お金がbelongs_to顧客である間のお金です。私は、リモートデータベースを使用しています。私はそれがうまく動作するが、私はこれを行うにしようとすると、問題がある時に、単一のテーブルからデータを抽出しようとすると:Ruby on Rails ActiveRecord :: StatementInvalid in Customers#show

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

それはエラーをスローします。mysql ::エラー:不明な列「money_tb。 customer_id 'in' where句: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

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

Thanks Alot #Michal_Mlozinak。それは働きました。ありがとうございました.... –

関連する問題