title: Simple Rails Join Example
body: <p>I'm trying to get what should be a basic table join to work.  This is my first time working with databases so there is a good chance I'm missing something simple.  I'm using Rails 3.2.13.</p>
  
  <p>I have two tables.  The first is feeds, and it contains a list of rss feeds and several other characteristics like a tag.  The second is feed_entries and it contains individual stories.  The column feed_entries.feed_name matches elements from the column feeds.name.  Here are the model files:</p>
  
  <pre><code>class Feed &lt; ActiveRecord::Base
    attr_accessible :color, :lastUpdated, :name, :posted, :read, :tag, :url
    has_many :feed_entries, foreign_key: "feed_name", primary_key: "name"
  end
  
  class FeedEntry &lt; ActiveRecord::Base
    attr_accessible :guid, :name, :published_at, :status, :summary, :url
    belongs_to :feed, foreign_key: "name", primary_key: "name"
  end
  </code></pre>
  
  <p>I don't understand foreign_key and primary_key, but based on what I've read I think they are set up correctly.</p>
  
  <p>Currently I can present all the entries of a certain feeds with FeedEntry.where(feed_name: 'BBC') or similar.  I'm trying to present the entries that match certain columns in the feeds table, like tag = Daily.  I've tried these commands:</p>
  
  <pre><code>Feed.joins(:feed_entries).where("feed_entries.tag = ?", 'Daily') #error
  FeedEntry.joins(:feed).where("feeds.tag = ?", 'Daily') #empty
  Feed.joins(:feed_entries).where(tag: 'Daily') #no columns from feed_entries
  </code></pre>
  
  <p>The first gives an error as there is no feed_entries.tag (it's a column in feeds).  The second returns no entries.  The third returns the proper number of entries, but only the columns present in the feeds table, not the more important feed_entries table.</p>
  
  <p>Can anyone explain what I'm missing?</p>