Rails Counter Cache


Counter Cache is a mechanism to cache the counts of associated model.Enabling counter cache will simply make Rails to increment and decrement the counter of associated objects. Whenever you need to execute size on ActiveRecord, Rails will use the cached counter.

Necessary steps.

  1. Add the counter_cache column as an integer.
  2. Make sure that the column is set to disallow null values and defaults to 0.
  3. Reset the counters for existing records. The reset_counters method is not smart enough to take an array of ids, so you have to call it for each record you want updated.

Example:

class Organization < ActiveRecord::Base
    has_many :seasons
  end

  class Season < ActiveRecord::Base
    belongs_to :organization, :counter_cache => true
  end

That's it.

Cheers!!!


Advertisements

Mongoid Keys, Convert BSON::ObjectId to string, string to BSON::ObjectId


1. Convert BSON::ObjectId to string

$profile = Profile.where(“profiles_manageds.profile_fname”=>’Admin’).first
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :077 >
1.9.3-p125 :078 >
1.9.3-p125 :079 >   profile
=> #<Profile _id: 4fe969dd79216d0af9000002, _type: nil, user_id: nil, organization_id: nil, email: nil, fname: “Ankit”, lname: “Mishra”, mname: “Kumar”, nickname: nil, gender: “Male”, birthdate: “15/05/1988”, firstRegistrationDate: nil, lastModifiedBy: nil, lastModifiedDate: “15/01/2011”, notes: nil, registeredById: nil, status: “active”, org_id: nil, measurements: nil, immunizations: nil, med_tests: nil, organizationsManaged: nil, parent_ids: nil, physicians: nil, profiles_manageds: [{“_id”=>BSON::ObjectId(‘4fe969dd79216d0af9000001’), “profile_id”=>”4fe9569979216d14ff000001”, “profile_fname”=>”Admin”, “profile_mname”=>”Kids”, “profile_lname”=>”link”}], registrationPayments: nil, sibling_ids: nil, organizationsAdministrated: nil, contacts: nil, default_emergencyContacts: nil, default_pickups: nil, documents: nil, child_extra_cares: nil, enrollments: nil, m_email: “”, m_fname: nil, m_lname: nil, m_phone_home: nil, m_phone_work: nil, m_phone_mobile: nil>
1.9.3-p125 :080 > profile.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :081 >
1.9.3-p125 :082 >
1.9.3-p125 :083 >   profile.id.to_s
=> “4fe969dd79216d0af9000002”

2. Convert string to BSON::ObjectId

$ profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :093 > profile_id = profile.id.to_s
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :094 >
1.9.3-p125 :095 >
1.9.3-p125 :096 >   profile_id
=> “4fe969dd79216d0af9000002”
1.9.3-p125 :097 >
1.9.3-p125 :098 >
1.9.3-p125 :099 >   BSON::ObjectId.from_string(profile_id)
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)