Activerecord加入不返回所有预期结果

时间:2015-03-13 18:26:32

标签: ruby-on-rails activerecord postgresql-9.3

我有一个预先存在的postgres数据库,我试图查询。我设置了两个模型,即客户和设备。我试图查询两个用非标准密钥连接它们的表。无论我尝试过什么,我只从一张桌子上取回结果。我尝试更改ActiveRecord行为以使用非标准密钥以及find_by_sql()。由于现有的表结构,我还禁用了默认表复数。我错过了一些明显的东西吗?

    ActiveRecord::Base.pluralize_table_names = false

    class Customer < ActiveRecord::Base
      self.primary_key = :acct
      has_many :equipment, foreign_key: 'acct'
    end

    class Equipment < ActiveRecord::Base
      self.primary_key = :acct
      belongs_to :customer, foreign_key: 'acct'
    end

    me@nc:~/rails_workspace/pg_test$ rails c
    Loading development environment (Rails 4.2.0.rc2)
    2.2.1 :001 > Customer.first()
      Customer Load (16.9ms)  SELECT  "customer".* FROM "customer"  ORDER BY "customer"."acct" ASC LIMIT 1
     => #<Customer acct: "000002", franchise: "501", name: "NAME", address: "STREET ADDR", city: "CITY", state: "ST", zip: "ZIPCODE", phone: "1234567890", email: "email@domain.com", installdate: "2014-02-20", consumptiondate: nil, parent: "", sentoverage: false, exempt: false, pin: "", locked: false, status: "ACT"> 
    2.2.1 :002 > Equipment.first()
      Equipment Load (128.3ms)  SELECT  "equipment".* FROM "equipment"  ORDER BY "equipment"."acct" ASC LIMIT 1
     => #<Equipment mac: "26225070", acct: "000002"> 
    2.2.1 :003 > 


    2.2.1 :009 > Customer.find_by_sql("Select c.*, e.* from customer as c, equipment as e where e.acct = c.acct and c.acct = '000002' limit 1")

    Customer Load (53.9ms)  Select c.*, e.* from customer as c, equipment as e where e.acct = c.acct and c.acct = '000002' limit 1
     => [#<Customer acct: "000002", franchise: "501", name: "NAME", address: "STREET ADDR", city: "CITY", state: "ST", zip: "ZIPCODE", phone: "1234567890", email: "email@domain.com", installdate: "2014-02-20", consumptiondate: nil, parent: "", sentoverage: false, exempt: false, pin: "", locked: false, status: "ACT">] 

    2.2.1 :010 > Customer.joins('FULL OUTER JOIN equipment ON equipment.acct = customer.acct').limit(1)
      Customer Load (119.9ms)  SELECT  "customer".* FROM "customer" FULL OUTER JOIN equipment ON equipment.acct = customer.acct LIMIT 1
     => #<ActiveRecord::Relation [#<Customer acct: "000002", franchise: "501", name: "NAME", address: "STREET ADDR", city: "CITY", state: "ST", zip: "ZIPCODE", phone: "1234567890", email: "email@domain.com", installdate: "2014-02-20", consumptiondate: nil, parent: "", sentoverage: false, exempt: false, pin: "", locked: false, status: "ACT">]> 

    2.2.1 :011 > Customer.joins(:equipment)
    ActiveRecord::ConfigurationError: Association named 'equipment' was not found on Customer; perhaps you misspelled it?
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:218:in `find_reflection'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:223:in `block in build'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:222:in `each'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:222:in `map'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:222:in `build'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/associations/join_dependency.rb:99:in `initialize'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation/query_methods.rb:1039:in `new'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation/query_methods.rb:1039:in `build_joins'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation/query_methods.rb:863:in `build_arel'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation/query_methods.rb:855:in `arel'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation.rb:638:in `exec_queries'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation.rb:514:in `load'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation.rb:243:in `to_a'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activerecord-4.2.0.rc2/lib/active_record/relation.rb:629:in `inspect'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/railties-4.2.0.rc2/lib/rails/commands/console.rb:110:in `start'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/railties-4.2.0.rc2/lib/rails/commands/console.rb:9:in `start'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/railties-4.2.0.rc2/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/railties-4.2.0.rc2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/railties-4.2.0.rc2/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:274:in `require'
    from /home/me/rails_workspace/pg_test/bin/rails:8:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/me/.rvm/gems/ruby-2.2.1@pg_test/gems/activesupport-4.2.0.rc2/lib/active_support/dependencies.rb:268:in `load'
    from /home/me/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/me/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.2.1 :012 >

修改

修改了设备类,将belongs_to:设备更改为:设备。导致修复ActiveRecord :: ConfigurationError:名为&#39;设备&#39;错误。但仍无法解决加入问题。如果我使用&#34;键&#34;手动查询Equipment表。在Customer表中,找到了数据记录。

2.2.1 :008 > Customer.joins(:equipment).limit(1)
  Customer Load (11.9ms)  SELECT  "customer".* FROM "customer" INNER JOIN "equipment" ON "equipment"."acct" = "customer"."acct" LIMIT 1
 => #<ActiveRecord::Relation > [#<Customer acct: "000002", franchise: "501", name: "NAME", address: "STREET ADDR", city: "CITY", state: "ST", zip: "ZIPCODE", phone: "1234567890", email: "email@domain.com", installdate: "2014-02-20", consumptiondate: nil, parent: "", sentoverage: false, exempt: false, pin: "", locked: false, status: "ACT">]

2.2.1 :017 > Equipment.where(acct: '000002')
  Equipment Load (76.5ms)  SELECT "equipment".* FROM "equipment" WHERE "equipment"."acct" = $1  [["acct", "000002"]]
 => #<ActiveRecord::Relation [#<Equipment mac: "26225070", acct: "000002">, #<Equipment mac: "23126536", acct: "000002">, #<Equipment mac: "00E06F76A08A", acct: "000002">]> 

2 个答案:

答案 0 :(得分:1)

对于更简单的解决方案,在scope模型中定义by_customer Equipment

class Equipment < ActiveRecord::Base
  ...
  scope :by_customer,->(customer_conditions) { joins(:customer).where(customer: customer_conditions)}    
end

并关注scope模型中的by_equipment Customer

class Customer < ActiveRecord::Base
  ...
  scope :by_equipment,->(equipment_conditions) { joins(:equipment).where(equipment: equipment_conditions)}
end
  

查找客户设备&#39; 000002&#39;

@equipments = Equipment.by_customer(acct: '000002')
  

要找到拥有mac地址设备的客户是&#39; 26225070&#39;

@customer = Customer.by_equipment(mac: '26225070')

答案 1 :(得分:0)

我想我可能会看到你缺少的东西。

Customer.joins(:anything)将返回Customer

的集合

Equipment.joins(:anything)将返回Equipment

的集合

也许你已经明白了,但也许不是。

如果你想要一个两者混合的结果集,我认为你必须完全从一个不同的角度来看待它,我想我需要更好地理解你最终的结果。为了能够提供任何好的建议。

修改以获取属于特定客户的设备:

Equipment.joins(:customer).where('customer.acct = ?', '000002')

为客户提供设备mac

Customer.joins(:equipment).where('equipment.mac = ?', '26225070')