Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use select() to name the columns you want. At least this works in Rails 3.0.9.</p> <p>Background: my application has a primary table named :rights. I wanted to be able to ascribe a tag and color to a given :right record so I could easily pick it out of an index listing. This doesn't cleanly fit the Rails picture of associated records; most :rights will never be tagged, and the tags are completely arbitrary (user input via tag/edit). </p> <p>I could try duplicating the tag data in the :right record, but that violates normal form. Or I could try querying :tags for each :right record, but that is a painfully inefficient approach. I want to be able to join the tables.</p> <p>MySQL console shows:</p> <pre><code>mysql&gt; describe rights; +------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | ... | Tagid | int(11) | YES | | NULL | | +------------+---------------+------+-----+---------+----------------+ mysql&gt; describe tags; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | TagName | varchar(255) | YES | | NULL | | | TagColor | varchar(255) | YES | | NULL | | | created_at | datetime | YES | | NULL | | | updated_at | datetime | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ </code></pre> <p>I am going to use TagName and TagColor in views/rights/index.html.erb, so I want the rights controller to include those columns in the @rights object it passes to the view. Since not every :right has a :tag, I want to use an outer join:</p> <pre><code>@rights = Right.joins("LEFT OUTER JOIN tags ON rights.Tagid = tags.id") </code></pre> <p>But, as everyone has found, this alone doesn't work: a block reference to TagName produces a server error. However, if I add a select at the end, all is well:</p> <pre><code>@rights = Right.joins("LEFT OUTER JOIN tags ON rights.Tagid = tags.id").select("rights.*,tags.TagName as TagName,tags.TagColor as TagColor") </code></pre> <p>Note added 6/7/13: the select clause does not require aliases - this works too:</p> <pre><code>.select("rights.*,tags.TagName,tags.TagColor") </code></pre> <p>Now I can reference TagName and TagColor in my view:</p> <pre><code>&lt;% @rights.each do |right| %&gt; &lt;tr ALIGN=Left &lt;%= # color background if this is tagged " BGCOLOR=#{right.TagColor}" if right.TagColor %&gt; &gt; ... &lt;% end %&gt; </code></pre>
    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.
    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