Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby ActiveRecord 2 level join
    primarykey
    data
    text
    <p>This should be a very basic question, I'm still new to ruby so i'd appreciate some help.</p> <p>So I have 3 tables in my db, Source, SourceType and Feed. Every Source belongs to a SourceType and Every Feed Belongs to a Source. Their primary keys are SourceID, TypeID and FeedID</p> <p>my Active Record Classes are:</p> <pre><code>class SourceFeed &lt; ActiveRecord::Base self.table_name = "SourceFeed" self.primary_key = "FeedID" belongs_to :Source, :foreign_key =&gt; "SourceID", :class_name =&gt; "Source", :include =&gt; "SourceType" end class Source &lt; ActiveRecord::Base self.table_name = "Source" self.primary_key = "SourceID" has_many :SourceFeeds, :primary_key =&gt; "SourceID", :class_name =&gt; "SourceFeed" belongs_to :SourceType, :foreign_key =&gt; "TypeID", :class_name =&gt; "SourceType" end class SourceType &lt; ActiveRecord::Base self.table_name = "SourceType" self.primary_key = "TypeID" has_many :Source, :primary_key =&gt; "TypeID", :class_name =&gt; "Source" end </code></pre> <p>I am trying to select stuff from SourceFeed, Stuff from Source and the SourceType. Here's the query:</p> <pre><code>feed = SourceFeed.select("SourceFeed.FeedID, Source.Name as SourceName, SourceType.Name as SourceType").joins(:Source, :SourceType).where(:FeedID =&gt; FeedID).first </code></pre> <p>I am getting a <code>Association named 'SourceType' was not found; perhaps you misspelled it?</code> Error</p> <p>If I remove it from the Joins, I get an <code>Unknown column 'SourceType.Name' in 'field list'</code> error.</p> <p>What's the right way to do it?</p> <p>Thanks</p> <p>PS: My database doesn't follow active records naming conventions, but I can't change that I am working with an existing DB.</p>
    singulars
    1. This table or related slice is empty.
    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