-1

我很难包装我的头如何构建此查询。我不确定是否应该尝试创建一堆范围并尝试链接它们。或者我把它放到类方法中?或者我会做两个组合吗?如果有人能给我一个简短的例子,它会阻止我跳出窗口,我已经为此工作了一个多星期了。复杂的ActiveRecord查询

class CensusDetail < ActiveRecord::Base 
    belongs_to :apartment 
    belongs_to :resident 
end 

class Apartment < ActiveRecord::Base 
    belongs_to :apartment_type 
    has_many :census_details 
    has_many :residents, :through => :census_details 
end 

class ApartmentType < ActiveRecord::Base 
    has_many :apartments 
end 

class Resident < ActiveRecord::Base 
    has_many :census_details 
    has_many :apartments, :through => :census_details 
end 

apartment.rent_ready  = boolean value if apartment is ready 
apartment_type.occupany = the number of allowed occupants in an apartment 
census_detail.status  = either "past", "present", or "new" 
census_detail.moveout_date = date time resident is scheduled to move out 

我需要建立一个查询,执行以下操作:

- if the apartment is rent ready then do the following: 
- pull a list from census_details of all residents set as "current" for each apartment 
    -if the # of residents is less than the value of apartment_type.occupancy for this 
    apartment, then list it as available 
    -if the # of residents is = to the value of apartment_type.occupancy then 
    -does any resident have a scheduled move out date 
     -if not, do not list the apartment 
     -if yes, then list apartment 

预先感谢任何帮助或建议。

+0

我试过使用范围和类方法。我明白如何创建两者,但仅限于更简单的查询。在这里,我首先获取公寓清单,然后我需要逐个遍历该清单,然后查询census_details,将那里找到的数字与apartment_type.occupancy中的值进行比较,从中做出更多决策。我不确定在使用示波器,方法或两者的组合之间使用正确的轨道方式来构建轨道。我不希望任何人为我写代码,更多的帮助如何解决这个问题。 –

+0

让我知道我是否在正确的轨道上,如果这是正确的方式,或者我弄得一团糟。这里是我现在在做的: –

+0

把范围放在CensusDetail范围内:resident_count, - >(id){where(“status =?and apartment_id =?”,“current”,id)}'创建一个名为可用的方法正在做上面列出的各种迭代,因为我正在构建它,如果有必要,我正在创建范围。我认为这会起作用。如果是的话,我会发布代码。如果我不在正确的轨道上,请告诉我。谢谢 –

回答

0

我还没有清理它,但这是为我工作,所以我想分享。

apartment_type.rb

class ApartmentType < ActiveRecord::Base 
    has_many :apartments, :dependent => :nullify 
end 

census_detail.rb

class CensusDetail < ActiveRecord::Base 
    belongs_to :resident_contacts 
    belongs_to :apartments 
    scope :get_current_residents, ->(id) { where("status = ? and apartment_id = ?", "current", id) } 
end 

apartment.rb其中大部分工作正在发生

class Apartment < ActiveRecord::Base 
    belongs_to :apartment_type 
    has_many :census_details 
    has_many :residents, :through => :census_details 
    scope :rent_ready,  -> { where(:enabled => true) } 

    def self.available 
    available_apartments = [] 
    rent_ready_apartments = self.rent_ready.all 

    rent_ready_apartments.each do |apt| 
     tmp = ApartmentType.where('id = ?', apt.apartment_type_id).pluck(:occupancy) 
     occupancy = tmp[0] 
     current_residents = CensusDetail.get_current_residents(apt.id) 
     resident_count = current_residents.count 

     if resident_count < occupancy 
     available_apartments << apt 
     end 

     if resident_count = occupancy 
     scheduled = false 
     current_residents.each do |res| 
      if res.moveout_date 
      scheduled = true 
      end 
     end 
     if scheduled == true 
      available_apartments << apt 
     end 
     end 
    end 
    available_apartments 
    end 
end 

的代码是丑了一点,但其如此远远超过我的测试。我会编辑我原来的问题,而不是回答以防万一有人有更好的方法来做这件事,但每次我做我的问题都会被拒绝。如果有人知道更好的方法,请让我知道,因为我在应用程序中有大约50个表的位置,现在它们都与复杂的查询结合在一起。