8

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.

ph3nx
  • 1,026
  • 2
  • 12
  • 25

1 Answers1

15

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
Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53
  • 2
    This makes sense but I want to understand exactly what to put in the rails generate command. Do you use: rails generate model Message user:references – thenextmogul Aug 11 '14 at 04:39
  • 3
    You can instead of putting references in the generator, place `sender_id` and `recipient_id` as integers, and let Rails do the wiring. – Sebastialonso Feb 01 '15 at 13:50