Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display multiple record of same type only once?
    primarykey
    data
    text
    <p>I have a rails 3.1.12 application, in which suppose i have a table <strong>A</strong> and in that i have some data and it is associated with table <strong>B</strong> with has_many relationship, i mean to say </p> <pre><code>class A &lt; ActiveRecord::Base has_many :B, :conditions =&gt; {:type =&gt; "B"}, :dependent =&gt; :destroy has_many :c, :class_name =&gt; "B", :conditions =&gt; {:type =&gt; nil}, :dependent =&gt; :destroy end </code></pre> <p>and in B's model i have :</p> <pre><code>class B &lt; ActiveRecord::Base belongs_to :A end </code></pre> <p>and i have some data in my table <strong>C</strong> which is inherited from <strong>B</strong></p> <pre><code> - **name** **amount** - c1 10 - c2 20 - c3 5 - c1 4 - c2 15 </code></pre> <p>and i want to display this duplicate items only one time something like:</p> <ul> <li><strong>name</strong> *<em>amount</em>* <ul> <li>c1 14</li> <li>c2 35</li> <li>c3 5</li> </ul></li> </ul> <p>But i don't have any idea how to get this, i have tried <code>**group_by**</code> and <code>uniq</code> methods but they are not working in my case.In my controller <strong>A</strong> on view action i have declare all this like:</p> <pre><code>class AsController &lt; ApplicationController def show @a = A.find(params[:id]) @bs = @a.bs @cs = @a.cs respond_to do |format| format.html # show.html.erb format.xml { render :xml =&gt; @a } end end end </code></pre> <p>and In my view file i have called all these like:</p> <pre><code>&lt;%= render :partial =&gt; "b_item_details", :collection =&gt; @bs %&gt; &lt;%= render :partial =&gt; "c_line_item_details", :collection =&gt; @cs %&gt; </code></pre> <p>I am sure some might faced such issue, any help would be thankful</p> <p>Actually i want to display unique records i mean if c1 has 4 entries and c2 has 3 entries than while displaying i want to display c1 and c2 only once and their total should be total of all their entries (c1+c1+c3+c4) </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