Rails 4 has many through and STI -


i assign teacher skills in case maths teacher i'm not sure if have correct set up. when try assign teacherskills teacher in console encounter errors show below.

i have used railscasts episode#17 can not clear these errors. can offer advice on correct set achieve has many through associations sti.

m = mathteacher.first m.math_teacher_skill_ids select `math_teacher_skils`.id `math_teacher_skills`  where`math_teacher_skills`.`math_teacher_id` = 1 => []  m.math_teacher_skill_ids = [1,2]  activerecord::recordnotfound: couldn't find mathteacherskills 'id': (1, 2) (found 0 results, looking 2)   class teacher < activerecord::base  end  class mathteacher < teacher has_many :math_teacher_skills has_many :skills, :through => :math_teacher_skills end  class skill < activerecord::base has_many :math_teacher_skills has_many :teachers, through: :math_teacher_skills end  class mathteacherskill < activerecord::base belongs_to :skill belongs_to :math_teacher end   #relevant schema create_table "math_teacher_skills", force: true |t| t.datetime "created_at" t.datetime "updated_at" t.integer  "math_teacher_id" t.integer  "skill_id" end  create_table "skills", force: true |t| t.datetime "created_at" t.datetime "updated_at" t.string   "name" end 

rails provides simpler way establish many-to-many relationship, has_and_belong_to_many. see more information here: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

if implement this, might this:

class mathteacher < teacher   has_and_belongs_to_many :skills end  class skill < activerecord::base   has_and_belongs_to_many :math_teachers end 

which call (assuming have created appropriate records in db):

m = mathteacher.first m.skills m.skills.pluck(:id) # if want ids 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -