Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_one on a belongs_to in Rails?
    primarykey
    data
    text
    <p>I am building a rails site and am having trouble with the associations. Basically I have the following:</p> <pre><code>class Publication &lt; ActiveRecord::Base belongs_to :category has_one :site, :through =&gt; :category named_scope :on_site, lambda {|s| {:include =&gt; [:site], :conditions =&gt; ['sites.slug != ?', 's']}} end class Category belongs_to :site has_many :publications end class Site has_many :categories has_many :publications, :through =&gt; :categories, :foreign_key =&gt; 'category_id' end </code></pre> <p><code>Publication.first.site</code> produces the first publication's site, <code>site.first.publications</code> does as well. </p> <p>The trouble is with the <code>on_site</code> named scope, which produces the following error with something like <code>Publication.on_site('s')</code></p> <pre><code>Mysql::Error: Unknown column 'categories.category_id' in 'on clause': SELECT `publications`.`id` AS t0_r0, `publications`.`shoot_id` AS t0_r1, `publications`.`category_id` AS t0_r2, `publications`.`title` AS t0_r3, `publications`.`slug` AS t0_r4, `publications`.`publish_on` AS t0_r5, `publications`.`created_at` AS t0_r6, `publications`.`updated_at` AS t0_r7, `publications`.`description` AS t0_r8, `publications`.`media_base_path` AS t0_r9, `sites`.`id` AS t1_r0, `sites`.`name` AS t1_r1, `sites`.`created_at` AS t1_r2, `sites`.`updated_at` AS t1_r3, `sites`.`slug` AS t1_r4, `sites`.`description` AS t1_r5, `sites`.`dhd_merch_id` AS t1_r6, `sites`.`members_area_url` AS t1_r7 FROM `publications` LEFT OUTER JOIN `categories` ON (`publications`.`id` = `categories`.`category_id`) LEFT OUTER JOIN `sites` ON (`sites`.`id` = `categories`.`site_id`) WHERE (sites.slug != 's') </code></pre> <p>I need that join to be publications.category_id = categories.id, any idea on what I've got wrong?</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