简体   繁体   中英

Rails 3 - work with data from few tables

I am solving a question, how to print for an item in one table appropriately items from second table.

In 'TabA' I have id: 1 2 3 4

In 'TabB' I have tab_a_id: 2 3 3 3 3 3 4

Specifically, my situation looks this:

as_controller

@info_a = TabA.all(:joins =>:bs, :conditions => ["city =?", 0])

view

  <% for info in @a %>
    <tr>
      <td><%= info.id %></td>
      <td><%= info.email %></td>
      <td><%= HERE I WOULD LIKE TO PRINT COUNT ITEMS FROM TabB ('bs' controller, 'b' model) %></td>
    </tr>
  <% end %>

And I am trying to found, how to print eg for item with ID number 3 from 'TabA' the count of lines in 'TabB' (in TabB is 5 times the value 3).

Thanks in advance!

EDIT: solution - info.tabb.size

Why don't you set up an association between A and B models? This way, you could do this: abcount .

The problem of your approach is that you request all the rows from the DB, Rails wraps them with AR objects, put them into array and than calculate the size of it. count on the other hand tells the DB to count the results and returns only one number so it is much more faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM