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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 September 2010 12:14, Daniel Morigan <daniel.morigan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > yDon''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> The result of this statement is a number and I need to show this > number in one collumn.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-09 13:03 UTC
Re: view on index pages data from custom SQL statements
named scopes http://railscasts.com/episodes/108-named-scope On Thu, Sep 9, 2010 at 8:37 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 9 September 2010 12:14, Daniel Morigan <daniel.morigan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 > > > The result of this statement is a number and I need to show this > > number in one collumn. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.