简体   繁体   English

Ruby on Rails:如何使用ActiveRecord对具有两个外键引用的表进行查询

[英]Ruby on Rails: How to do a query on a table having two foreign key references with ActiveRecord

I have three models: Customer, Bank and Account. 我有三种模式:客户,银行和账户。 each customer can have many accounts, so does a bank. 每个客户都可以拥有多个账户,银行也是如此。

class Customer < ActiveRecord::Base
has_many :Accounts

class Bank < ActiveRecord::Base
has_many :Accounts

Account < ActiveRecord::Base
belongs_to :Customer, :foreign_key => 'customerID'
belongs_to :Bank, :foreign_key => 'bankID'

If I want to find all accounts for customer Jack, I can do 如果我想找到客户杰克的所有账户,我可以做到

Customer.find_by_name('jack').Accounts

If I want to find all accounts for Citi bank, then I can do query like 如果我想查找花旗银行的所有账户,那么我可以查询

Bank.find_by_name('Citi').Accounts

My question is how can I find the account for Customer Jack which belongs to the Citi bank with ActiveRecord? 我的问题是如何通过ActiveRecord找到属于Citi银行的Customer Jack帐户? There is some way to explicitly generate a SQL statement but I wonder how can I do similar queries for other models having the same relationship in a generic way. 有一些方法可以显式生成SQL语句,但我想知道如何以通用方式对具有相同关系的其他模型执行类似的查询。

accounts = Account.joins(:bank, :customer)
                  .where( banks: { name: "Citi" }, customers: { name: "Jack" } )

I think I've got the plurals bank/banks, customer/customers the right way round. 我想我的复数银行/银行,客户/客户是正确的。 If it doesn't work first time, try it in the console - build it up by stages, the joins first, then the where bit. 如果它第一次不起作用,请在控制台中尝试 - 按阶段构建,首先连接,然后在where位。

This has the advantage of being only one SQL call. 这样做的好处是只能进行一次SQL调用。

The rails query guide is very useful - http://guides.rubyonrails.org/active_record_querying.html rails查询指南非常有用 - http://guides.rubyonrails.org/active_record_querying.html

bank = Bank.find_by_name('City')
accounts = Customer.find_by_name('jack').accounts.where(:bank_id => bank.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在具有外键的表中插入记录-Ruby on Rails - Inserting a record in a table having foreign key - Ruby on Rails Rails ActiveRecord如何在第二张表中做外键? - Rails ActiveRecord How to make foreign key in second table? Ruby on Rails:如何使用ActiveRecord对两列进行排序? - Ruby on Rails: how do I sort with two columns using ActiveRecord? Ruby on Rails ActiveRecord:外键和主键的急切加载问题 - Ruby on Rails ActiveRecord: eager loading issue with foreign and primary key 在 Ruby on Rails 中,如何保存许多引用同一类的其他实例的 ActiveRecord 对象? - In Ruby on Rails, how do I save many ActiveRecord objects that have references to other instances of the same class? 如果外键存在于 Ruby on Rails 中,我该如何删除它? - How do I drop a foreign key if it exists in Ruby on Rails? &#39;未知密钥:拥有&#39;在Ruby on Rails上的ActiveRecord查找方法 - 'Unknown key(s): having' In Ruby on Rails ActiveRecord Find Method 从表中删除外键-Ruby on Rails - Remove foreign key from a table - Ruby on rails 从表中删除主键,在rails中使用外键记录引用 - Remove primary key from a table the references record with foreign key in rails 如何查询以使用Rails返回联接表中最常见的外键 - How to query to return the most common foreign key in a join table with rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM