2010-05-10 101 views
2

我在Ruby on Rails中創建站點,我有兩個模型User模型和Transaction模型。Ruby on Rails - 主鍵和外鍵

這些模型都屬於一個帳戶,以便它們都具有一個名爲場account_id

我試圖建立它們之間的關聯,像這樣:

class User < ActiveRecord::Base 
    belongs_to :account 
    has_many :transactions 
end 

class Transaction < ActiveRecord::Base 
    belongs_to :account 
    belongs_to :user 
end 

我使用這些協會,如下所示:

user = User.find(1) 
transactions = user.transactions 

目前的應用程序試圖找到與user_id的交易,這裏是它生成的SQL:

Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 1) 

這是不正確的,我想通過account_id中找到交易,我已經嘗試設置像這樣的關聯:

class User < ActiveRecord::Base 
    belongs_to :account 
    has_many :transactions, :primary_key => :account_id, :class_name => "Transaction" 
end 

class Transaction < ActiveRecord::Base 
    belongs_to :account 
    belongs_to :user, :foreign_key => :account_id, :class_name => "User" 
end 

這幾乎達到我所希望做的,併產生下面的SQL:

Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 104) 

數量104是正確account_id,但它仍試圖查詢交易表爲user_id字段。可能有人給我上的關聯如何我設置查詢事務表爲account_id而不是user_id導致SQL查詢,像這樣一些建議:

SELECT * FROM `transactions` WHERE (`transactions`.account_id = 104) 

乾杯

EEF

回答

0

如果您在transactions ta中沒有列user_id ta BLE那麼你應該使用Account模型選擇所有交易:

class User < ActiveRecord::Base 
    belongs_to :account 
end 

class Account < ActiveRecord::Base 
    has_many :transactions 
    has_one :user # or has_many :users 
end 

class Transaction < ActiveRecord::Base 
    belongs_to :account 
end 

User.find(1).account.transactions 

請注意,你必須從Userbelongs_to :userTransaction刪除has_many :transactions,因爲他們認爲這樣做有user_id列。

-1

我要存儲地址模型與學生模型協會

class AddressesController < ApplicationController 

    def new 
     @address = Address.new  
    end 

    def create 
     @address = Address.create(:address_date => Time.now, 
      :student_id => @student.id) 
    end 

    private 

    def address_params 
     params.require(:address).permit(:gali_no, :house_no_flate_no, :vill_town_city, :district, :state, :post_code, :country) 
    end 
end 






class StudentsController < ApplicationController 

    def show 

    end 

    def new 
     @student = Student.new 
    end 

    def create 
     @student = Student.new(student_params) 

     if @student.save 
      redirect_to root_url, :notice => "You have been registered" 
     else 
      render "new" 
     end 
    end 

    private 
    def student_params 
     params.require(:student).permit(:f_name, :l_name, :email, :password, :password_confirmation, :mobile_no) 
    end 
end 


class Student < ActiveRecord::Base 

    has_many :addressess, :dependent => :destroy 

    attr_accessor :password 

    before_save :encrypt_password 

    validates_confirmation_of :password 
    validates_presence_of :f_name 
    validates_presence_of :l_name 
    validates_presence_of :password, :on => :create 
    validates_presence_of :email 
    validates_uniqueness_of :email 

    def encrypt_password 
     if password.present? 
      self.password_salt = BCrypt::Engine.generate_salt 
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
     end 
    end 
end 




class Address < ActiveRecord::Base 

    belongs_to :student 

end 
+0

你應該合理適當語法高亮和縮進構建答案數據。 – RatDon 2015-03-20 05:31:06