简体   繁体   English

如何根据滑轨中的(表Y /列Z)定义(表A /列X)?

[英]How to define (table A/column X) according to (Table Y/column Z) in rails?

Guys I need to make it like this: 伙计们,我需要这样:

I have two tables 我有两张桌子

Users table with column name 名称的 用户

and

Profiles table with column profilename 配置文件表列PROFILENAME

I need to make it so that when user is created and name is given to a user, It also creates Profile and gives it profilename same as user's name. 我需要这样做,以便在创建用户并为用户指定名称时,它还会创建Profile,并为其提供与用户名相同的profilename。

Thanks in advance :) 提前致谢 :)

Have a profile which belongs to the user so it'd have a user_id field. 有一个属于用户的配置文件,因此它有一个user_id字段。 Then use the after_create callback and associations 然后使用after_create回调和关联

class User

  has_one :profile
  after_create :make_profile

  def make_profile
    create_profile(:profilename => name)
  end

end

class Profile
  belongs_to :user
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM