Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a complete answer to this issue, in case people visiting this question are new to Ruby on Rails and having a hard time putting everything together (as I was when I first looked into this).</p> <p>Some parts of the solution take place in your Migrations and some in your Models:</p> <h2>Migrations</h2> <pre><code>class CreatePrivateMessages &lt; ActiveRecord::Migration def change create_table :private_messages do |t| t.references :sender t.references :recipient end # Rails 5+ only: add foreign keys add_foreign_key :private_messages, :users, column: :sender_id, primary_key: :id add_foreign_key :private_messages, :users, column: :recipient_id, primary_key: :id end end </code></pre> <p>Here you are specifying that there are two columns in this table that will be referred to as :sender and :recipient and which hold references to another table. Rails will actually create columns called 'sender_id' and 'recipient_id' for you. In our case they will each reference rows in the Users table, but we specify that in the models, not in the migrations.</p> <h2>Models</h2> <pre><code>class PrivateMessage &lt; ActiveRecord::Base belongs_to :sender, :class_name =&gt; 'User' belongs_to :recipient, :class_name =&gt; 'User' end </code></pre> <p>Here you are creating a property on the PrivateMessage model named :sender, then specifying that this property is related to the User class. Rails, seeing the "belongs_to :sender", will look for a column in your database called "sender_id", which we defined above, and use that to store the foreign key. Then you're doing the exact same thing for the recipient.</p> <p>This will allow you to access your Sender and Recipient, both instances of the User model, through an instance of the PrivateMessage model, like this: </p> <pre><code>@private_message.sender.name @private_message.recipient.email </code></pre> <p>Here is your User Model:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :sent_private_messages, :class_name =&gt; 'PrivateMessage', :foreign_key =&gt; 'sender_id' has_many :received_private_messages, :class_name =&gt; 'PrivateMessage', :foreign_key =&gt; 'recipient_id' end </code></pre> <p>Here you are creating a property on the User Model named :sent_private_messages, specifying that this property is related to the PrivateMessage Model, and that the foreign key on the PrivateMessage model which relates it to this property is called 'sender_id'. Then you are doing the same thing for received private messages.</p> <p>This allows you to get all of a users sent or received private messages by doing something like this:</p> <pre><code>@user.sent_private_messages @user.received_private_messages </code></pre> <p>Doing either of these will return an array of instances of the PrivateMessage model.</p> <p>....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload