0

I want to figure out what is checked with this method and then I will test according to this.

partner.rb

def get_record_count
    self.administrator? ? ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id).size : self.contact_record.size
  end
4

2 回答 2

1

If that partner is an administrator then it matches it's company_id with fellow partners. Then it retrieves associated ContactRecords where partner_id = fellow ids and return back the size of total such records : number of matched contact records.

If it is not an administrator, it gives back it's contact_record.size

ActiveRecord substitute for:
ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ?)", self.company_id).size

ContactRecord.where(:partner_id => Partner.where(:partner_id => self.company_id).pluck(:id)).size

于 2013-05-16T15:43:07.073 回答
0

1.

SELECT id FROM partners WHERE company_id = ?

This part returns list of ids of "partners" with company_id = id of current Object (Model). Name this list as PARTNERS_LIST

2.

ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id)

And this part finds ContactRecords with "partner_id" belongs to PARTNERS_LIST.

3.

self.administrator? ? ContactRecord.where("partner_id in ( SELECT id FROM partners WHERE company_id = ? )",self.company_id).size

Finally this returns count of ContactRecords, finded at stage 2.

于 2013-05-16T15:52:14.907 回答