ruby on rails - Populate a thrid model, pulling data from different model with an after_create callback on User model -


i have 3 models, user, member_role & member. need use after_create callback on user model , populate member model fetching role member_role model.

would appreciate regarding how can accomplish that.

my member_role model loos like

1 - admin 2 - teacher 3- student 4 - on

and mr member model looks like

member_id user_id member_role_id

need populate member model after_create callback on user model.

here schema.rb

activerecord::schema.define(version: 20150930061155)    create_table "member_roles", force: :cascade |t|     t.string   "role",       limit: 255     t.datetime "created_at",             null: false     t.datetime "updated_at",             null: false   end    create_table "members", force: :cascade |t|     t.datetime "created_at",               null: false     t.datetime "updated_at",               null: false     t.integer  "user_id",        limit: 4     t.integer  "member_role_id", limit: 4   end    create_table "users", force: :cascade |t|     t.string   "provider",               limit: 255,   default: "email", null: false     t.string   "uid",                    limit: 255,   default: "",      null: false     t.string   "encrypted_password",     limit: 255,   default: "",      null: false     t.string   "reset_password_token",   limit: 255     t.datetime "reset_password_sent_at"     t.datetime "remember_created_at"     t.integer  "sign_in_count",          limit: 4,     default: 0,       null: false     t.datetime "current_sign_in_at"     t.datetime "last_sign_in_at"     t.string   "current_sign_in_ip",     limit: 255     t.string   "last_sign_in_ip",        limit: 255     t.string   "confirmation_token",     limit: 255     t.datetime "confirmed_at"     t.datetime "confirmation_sent_at"     t.string   "unconfirmed_email",      limit: 255     t.string   "name",                   limit: 255     t.string   "nickname",               limit: 255     t.string   "image",                  limit: 255     t.string   "email",                  limit: 255     t.text     "tokens",                 limit: 65535     t.datetime "created_at"     t.datetime "updated_at"   end    add_index "users", ["email"], name: "index_users_on_email", using: :btree   add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree   add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree  end 

assuming default memberrole first one:

class user   after_create :assign_member_role    private   def assign_member_role     mr = memberrole.first # default mr     member.create!(user_id: self.id, member_role_id: mr.id)   end end 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -