简体   繁体   English

rails-帮助设置模型-has_one -belongs_to

[英]rails - help setting up models - has_one - belongs_to

I've quite a simple query regarding models. 我有一个关于模型的简单查询。

I have a model - 我有一个模特-

class User < ActiveRecord::Base
  has_one :interest
end

And another - 还有一个-

class Interest < ActiveRecord::Base
  belongs_to :donor
end

My Interest table is prepolated with interests - "football", "swimming", "tennis" Each user can only have one interest, but an interest can have many users. “我的兴趣”表中有兴趣-“足球”,“游泳”,“网球”。每个用户只能有一个兴趣,但是一个兴趣可以有许多用户。

My set up at the moment seems to allow an interest to only have one user. 我目前的设置似乎只允许一个用户拥有一个兴趣。 What am I doing wrong? 我究竟做错了什么?

Simply set has_many on Interest and belongs_to on User . 只需设置has_manyInterestbelongs_toUser Remember about Rails convention in naming things: 在命名时,请记住有关Rails约定的内容:

belongs_to :interest

..and... ..和...

has_many :users

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

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