# View on index pages data from custom SQL statements

**URL:** <https://www.ruby-forum.com/t/view-on-index-pages-data-from-custom-sql-statements/194715>\
**Category:** Rails\
**Created:** [September 9, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/view-on-index-pages-data-from-custom-sql-statements/194715 "2010-09-09T13:14:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 9, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/view-on-index-pages-data-from-custom-sql-statements/194715/1 "2010-09-09T13:14:58Z")

</div>

I need to show on my index page in one of the collumns data that would  
be result of a SQL statment. Is there a universal way to do it? In my  
current case I have table A and B, A has many B, B belongs to A. In my  
A’s index page I need to have data that would be the result of such  
SQL statement:  
select count(\*) from B where A\_id=x and B.number \> y  
The result of this statement is a number and I need to show this  
number in one collumn.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 9, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/view-on-index-pages-data-from-custom-sql-statements/194715/2 "2010-09-09T14:41:03Z")

</div>

On 9 September 2010 12:14, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I need to show on my index page in one of the collumns data that would  
> be result of a SQL statment. Is there a universal way to do it? In my  
> current case I have table A and B, A has many B, B belongs to A. In my  
> A’s index page I need to have data that would be the result of such  
> SQL statement:  
> select count(\*) from B where A\_id=x and B.number \> y

Don’t use an sql statement, use the ActiveRecord API. Assuming that  
you have an A object, an\_a, then something like  
the\_count = an\_a.bs.find( :all, :conditions =\> { :number =\> x } ).count  
should do the trick (untested).

Colin

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 9, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/view-on-index-pages-data-from-custom-sql-statements/194715/3 "2010-09-09T15:04:29Z")

</div>

named scopes

> **[\#108 named\_scope - RailsCasts](http://railscasts.com/episodes/108-named-scope)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
