Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 832
ph3nx Avatar asked Jan 03 '14 17:01

ph3nx


1 Answers

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
like image 163
Said Kaldybaev Avatar answered Nov 15 '22 00:11

Said Kaldybaev