I am creating active record associations for the first time and would like to understand what to expect after I have added them. I am using rails 3.2.14
I have 3 tables Users, Applications and Jobs
1] In application table I have added User_Id and Job_id as the foreign key column, since an application belongs to a User and a Job.
2] I have also added the necessary associations.
3] After adding the foreign key columns and the associations, is running rake db:migrate sufficient or do I have to do anything else to complete the relational modeling ?
Some Context
Now when I create a new application after clicking on a job link, I was hoping that the job_id and user_id would be automagically populated by the framework in Application:create method where it performs
Application.new(params[:application])
However , seems like the foreign_key columns are not populated by the framework and I would have to manually extract the relevant job_id and user_id in the create method and populate it to the application.
One advantage of not adding associations is if I later I realize some relation is has_and_belong_to_many instead of has_many I can just handle that in the tables since no associations are 'frozen' at that point.
I have following questions:
1]what was the benefit of defining a associations in the model. I could have just added foreign key columns and handled them without associating them in models.
2] What other things I should expect (would make my life easier eventually) by adding associations in the models ?
Here are the models:
Models:
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :user_id
validates_presence_of :email
end
class Job < ActiveRecord::Base
attr_accessible :company, :desc, :location, :application_id, :applicant_id
belongs_to :recruiters, :class_name => "User"
has_many :applications
has_many :applicants,:class_name => "User", through: :applications
end
class Application < ActiveRecord::Base
attr_accessible :applicant_email, :applicant_name, :recruiter_id, :applicant_id, :user_id
belongs_to :jobs
belongs_to :applicants, :class_name => "User"
end
Note Feel free to provide links to videos, blogs in the comment section, where these things are explained in a simple manner . I did not find the the official doc that helpful to understand all steps, http://guides.rubyonrails.org/association_basics.html and there is no relevant rails-cast I can find