简体   繁体   English

Ruby on Rails:两个对同一模型具有不同名称的引用

[英]Ruby on Rails: two references with different name to the same model

My app has a model called User (it includes the email adress, username..) I want to create a model Message it should have two fields sender and recipient . 我的应用程序有一个名为User的模型(它包括电子邮件地址,用户名..)我想创建一个模型Message它应该有两个字段senderrecipient Both are references to the User model. 两者都是对User模型的引用。 I tried this: 我试过这个:

rails generate model Message sender:references recipient:references

Rails generated this: Rails生成了这个:

class Message < ActiveRecord::Base
  belongs_to :sender
  belongs_to :recipient
end

But I don't want two different models. 但我不想要两种不同的型号。 Both fields should reference to User . 这两个字段都应引用User I'm running Ruby 2.0.0 and Rails 4.0.2. 我正在运行Ruby 2.0.0和Rails 4.0.2。 Any help is highly appreciated. 任何帮助都非常感谢。 Please ask me if you need more information about my problem. 如果您需要有关我的问题的更多信息,请询问我。

You can specify the class name of the association, doc 您可以指定关联的类名doc

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User'
  belongs_to :recipient, class_name: 'User'
end

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

相关问题 同一模型在另一个Rails中的两个引用:4 - Two references of the same model in another Rails: 4 如何使用来自同一Rails模型上两个不同宝石的具有相同“名称”的两种不同方法? - How to use two different method with the same “name” from two different gems on the same rails model? Ruby on Rails:两个has_many same_models:through =&gt; different_model - Ruby on Rails: two has_many same_models :through => different_model Rails找不到与Ruby类同名的模型 - Rails cannot find model with same name as Ruby class Ruby on Rails中具有不同关联的同一模型的多种形式 - Multiple forms for the same model with different associations in Ruby on Rails 在Ruby on Rails中,如何创建一个表/模型,其中包含引用同一表的字段? - In Ruby on Rails, how do I create a table/model that has a field in it that references the same table? 如何在一个表中添加对同一模型的多个引用的迁移? 红宝石/导轨 - How do I add migration with multiple references to the same model in one table? Ruby/Rails 在 ruby​​ on rails 中访问不同文件中同名的模块/类 - Accessing a module/class with same name in a different file in ruby on rails 模型中的两个fileld引用相同的模型 - Two filelds in model with references to the same model 将两个不同的字符串与Ruby on Rails中的相同部分进行比较 - Compare two different string with the same part in Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM