Wednesday, October 3, 2012

Save data with has_many :through on Rails

The key is to put "attr_accessible" on the join model.

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient

  attr_accessible :physician_id, :patient_id
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
end

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.