首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails -主键和外键

问Ruby on Rails -主键和外键
EN

Stack Overflow用户
提问于 2010-05-10 18:26:42
回答 3查看 8.7K关注 0票数 2

我正在用Ruby on Rails创建一个站点,我有两个模型,一个是User模型,一个是Transaction模型。

这两个模型都属于一个帐户,因此它们都有一个名为account_id的字段

我尝试在它们之间建立一个关联,如下所示:

代码语言:javascript
复制
class User < ActiveRecord::Base
  belongs_to :account
  has_many :transactions
end

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

我像这样使用这些关联:

代码语言:javascript
复制
user = User.find(1)
transactions = user.transactions

此时,应用程序正在尝试使用user_id查找事务,下面是它生成的SQL:

代码语言:javascript
复制
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 1)

这是不正确的,因为我希望通过account_id查找交易,我已经尝试像这样设置关联:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Mysql::Error: Unknown column 'transactions.user_id' in 'where clause': SELECT * FROM `transactions` WHERE (`transactions`.user_id = 104)

编号104是正确的account_id,但它仍在尝试查询事务表中的user_id字段。谁能给我一些建议,关于我如何设置关联来查询transaction表中的account_id,而不是导致如下SQL查询的user_id:

代码语言:javascript
复制
SELECT * FROM `transactions` WHERE (`transactions`.account_id = 104)

干杯

Eef

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-05-10 18:36:29

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2010-05-10 18:39:27

如果transactions表中没有列user_id,那么应该使用Account模型来选择所有事务:

代码语言:javascript
复制
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

请注意,您必须从User中删除has_many :transactions,从Transaction中删除belongs_to :user,因为它们假定您确实有user_id列。

票数 0
EN

Stack Overflow用户

发布于 2015-03-20 13:12:20

我想将数据存储在与学生模型相关联的地址模型中

代码语言:javascript
复制
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
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2801900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档