Posts

Showing posts from April, 2015

When to choose between has_and_belongs_to_many and has_many, through association

Image
This was really confusing for me when to choose has_and_belongs_to_many and when to choose has_many , through association because both sets up many-to-many relationship between tables. Qns:  When to choose has_and_belongs_to_many association . Ans: The answer is when we does not need any kind of information about the association or the information about join model then we should use has_and_belongs_to_many association. Example: An assembly can have many parts and a part can have many assemblies and we don't need any other information about association then sets up direct many-to-many association between tables. via another table called assemblies_parts and it should have the foreign keys for both the table. Qns: When to choose has_many, through association.   Ans: The answer is when we need the information about the association or the information about join model then we should use has_many ,through association. Example: A physician can have many

Exploring the :inverse_of option on rails model association

 Let's start with the basic models and defining inverse of relationship in these models: class Criminal < ActiveRecord::Base   belongs_to :prison, inverse_of: :criminals end class Prison < ActiveRecord::Base   has_many :criminals, inverse_of: :prison end  Memory Optimization When Fetching Associated Records It turns out that associated objects do not point to the same in-memory objects by default. To illustrate: prison   = Prison.create(name: 'Bad House') criminal = prison.criminals.create(name: 'Krazy 8')   # Without :inverse_of criminal.prison == prison # Prison Load (0.1ms)  SELECT "prisons".* FROM "prisons" WHERE "prisons"."id" = 2 LIMIT 1 # => true   # With :inverse_of criminal.prison == prison # => true When we call criminal.prison without :inverse_of on both the :belongs_to and :has_many associations, it will hit the database. With :inverse_of , if we already have that prison record

Executing rake task through Cronjob in rails for differnet environments

Cronjob : The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. Cronjob's are different for every user You can check your cronjob as > crontab -l or you can edit your crontab in one of your editor > EDITOR=nano crontab -e A file would open like this:  # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion