Ruby on Rails - 主键和外键

时间:2010-05-10 10:26:42

标签: ruby-on-rails ruby activerecord associations

我在Ruby on Rails中创建一个网站,我有两个模型User模型和Transaction模型。

这些模型都属于某个帐户,因此它们都有一个名为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

3 个答案:

答案 0 :(得分:3)

class Account < ActiveRecord::Base
  has_many :users
  has_many :transactions
end

class User < ActiveRecord::Base
  has_many :transactions, :through => :account
end

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001833

答案 1 :(得分:0)

如果user_id表中没有columnt transactions,那么您应该使用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

请注意,您必须从has_many :transactionsUser移除belongs_to :user,因为他们认为您确实拥有Transaction列。

答案 2 :(得分:-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