简体   繁体   中英

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 . Both are references to the User model. I tried this:

rails generate model Message sender:references recipient:references

Rails generated this:

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 . I'm running Ruby 2.0.0 and 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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