Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Changes Related to Active Records While Updating From Rails 3.2 to Rails 4.0

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 583
    Comment on it

    From Rails 3.2 to 4.0, there were so many things that got changed. So here I am telling you the major changes that affected the active records, and that you should take care of during updation from 3.2 to 4.0:


    1) There were some inconsistencies reported in 3.2 regarding the associations, which are fixed in 4.0. So for that identity mapping has been removed. So if identity mapping is enabled in your application, then it will be of no use now. It is needed to remove its setting from the config. So remove the following line from you config, if it is present

    config.active_record.identity_map


    2) In rails 4.0, the delete method used earlier has been modified a little, so now it also accepts integer or strings as the id of the record, just like the destroy method. In the previous versions, it used to throw the following error:

    ActiveRecord::AssociationTypeMismatch

    3) In the rails version before 4, whenever you rename the column or table name, it was required to rename the indexes as well, so generally you needed to mention that in migrations, but from rails 4, indexes for column or tables are automatically renamed, whenever the column name or table name is renamed, so if you are using earlier version and you have migrations for such task, you can remove them.


    4) One more major change is that from now on serialized_attributes and attr_readonly are only class methods, so if you are using it as an instance methods, you need to convert them so that they can be used:

    self.serialized_attributes => self.class.serialized_attributes
    self.attr_readonly => self.class.attr_readonly

    5) In Rails 4.0 attr_accessible and attr_protected are no longer used, So if you want a smooth upgradation, you can use Protected Attributes gem. You can also use whitelist_attributes or mass_assignment_sanitizer options.

    6) The syntax for scope, also got changed a little, now it can be used as a callable object. i.e:

    scope :in_progress, where(deleted: false)
     
    # has been changed to
    
    scope :in_progress, -> { where deleted: false }

    7) Rails 4 has also changed the default naming conventions for the joins tables of has_and_belongs_to_many associations. So now if you are using the older one you need to manually specify the join table name:

    HospitalDoctor < ActiveRecord::Base
      has_and_belongs_to_many :hospital_patients, join_table: 'hospital_doctors_hospital_patients'
    end
     
    HospitalPatient < ActiveRecord::Base
      has_and_belongs_to_many :hospital_doctors, join_table: 'hospital_doctors_hospital_patients'
    end

    8) Other deprecation related changes are that now ActiveRecord::Fixtures has been compromised with ActiveRecord::FixtureSet and ActiveRecord::TestCase is compromised with ActiveSupport::TestCase.


    9) Now last but not least, most of the finders excepting find_by_... and find_by_...! are deprecated from now on. Few of them you can handle as given below:

    find_all_by_... becomes where(...).
    find_last_by_... becomes where(...).last.
    scoped_by_... becomes where(...).
    find_or_initialize_by_... becomes find_or_initialize_by(...).
    find_or_create_by_... becomes find_or_create_by(...).

    Hope you liked reading this.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: