Note that there are some explanatory texts on larger screens.

plurals
  1. POShould this even be a has_many :through association?
    primarykey
    data
    text
    <p>A Post belongs_to a User, and a User has_many Posts.<br> A Post also belongs_to a Topic, and a Topic has_many Posts.</p> <pre><code>class User &lt; ActiveRecord::Base has_many :posts end class Topic &lt; ActiveRecord::Base has_many :posts end class Post &lt; ActiveRecord::Base belongs_to :user belongs_to :topic end </code></pre> <p>Well, that's pretty simple and very easy to set up, but when I display a Topic, I not only want all of the Posts for that Topic, but also the user_name and the user_photo of the User that made that Post. However, those attributes are stored in the User model and not tied to the Topic. So how would I go about setting that up?</p> <p>Maybe it can already be called since the Post model has two foreign keys, one for the User and one for the Topic?</p> <p>Or, maybe this is some sort of "one-way" has_many through assiociation. Like the Post would be the join model, and a Topic would has_many :users, :through => :posts. But the reverse of this is not true. Like a User does NOT has_many :topics.</p> <p>So would this even need to be has_many :though association? I guess I'm just a little confused on what the controller would look like to call both the Post and the User of that Post for a give Topic.</p> <p>Edit: Seriously, thank you to all that weighed in. I chose tal's answer because I used his code for my controller; however, I could have just as easily chosen either j.'s or tim's instead. Thank you both as well. This was so damn simple to implement. I think today might mark the beginning of my love affair with rails.</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.
 

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