How to create rails association -
how create rails associations this: 
and example show this: 
i'd assume have users table first, , each user object has_one or has_many skill_set or skill_sets respectively.
table name: users id first_name last_name address 1 arslan ali abc 2 adnan saqib abc 3 mahtab maqsood abc and table, skill_sets:
table name: skill_sets id user_id dance_skill sing_skill model_skill 1 1 1 2 2 2 3 2 2 3 3 2 1 1 2 and, in rails, model following:
class user < activerecord::base has_one :skill_set # i'd go 1 skill set per user end class skillset < activerecord::base belongs_to :user # it'd have 'user_id' in it. end for migrations, i'd have like:
class createusers < activerecord::migration def change create_table :users |t| t.stirng first_name t.string last_name t.string address t.timestamps null: false end end end class createskillsets < activerecord::migration def change create_table :skill_sets |t| t.references :user, index: true t.integer :dance_skill, null: false, default: 1 t.integer :sing_skill, null: false, default: 1 t.integer :model_skill, null: false, default: 1 end end end and don't understand, why using table skills if have store 3 rows. can have following in model:
class skillset < activerecord::base skill_level = ["good","profi","noob"] def skill_level(number) skill_level[number-1] # arrays based on zero-index. end # can user.skill_level(user.dance_skill) # other way: can 'enums' well, you. end
Comments
Post a Comment