在 Ruby on Rails 中链接模型 [英] Linking models in Ruby on Rails
问题描述
我正在构建一个 Rails 应用,我想连接两个模型.
I"m building a Rails app, and there are two models that I want to connect.
有一个称为用户"的模型来处理用户身份验证,例如用户名和密码数据.然后是一个称为个人资料"的模型,其中包含一个人的位置、描述等.
There's a model called "users" that handles user authentication, e.g. username and password data. Then there's a model called "profiles" that has a person's location, description, etc, etc.
我想链接模型,以便每个用户有一个个人资料.我真的不知道该怎么做.有人介意分享一些关于如何做到这一点的见解吗?
I want to link the models so that there's one profile per user. I really don't know how to do this. Anyone mind sharing some insight on how this can be done?
推荐答案
查看 rails 协会基础知识
你的协会看起来像
class User < AR:Base
has_one :profile
end
class Profile < AR:Base
belongs_to :user
end
哦,表配置文件必须有一个 user_id 列,其中包含用户的外键.
oh, the table profiles must have a user_id column with the foreign key to the user.
这篇关于在 Ruby on Rails 中链接模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!