2010-05-10 69 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