hello,
I have the following in my controller
def totalmembers
@total = User.find_by_sql "SELECT COUNT(*) as count FROM users "
end
now inside my template how do i access the field "count" from the
query so i can print out the number of members ?
I tried everythign like <%= @total.count %> but nothing works.
thanks
adam
Adam Denenberg wrote:> I have the following in my controller > > def totalmembers > @total = User.find_by_sql "SELECT COUNT(*) as count FROM users " > end > > now inside my template how do i access the field "count" from the query > so i can print out the number of members ? > I tried everythign like <%= @total.count %> but nothing works.Hi Adam, If you do def total_members @count = User.count # @count will be set to an integer, such as 10 end Then your template can contain There are <%= @count %> members signed up. You might want to use the pluralize helper to use ''member'' when appropriate. (Look it up on http://rails.rubyonrails.com/fr_method_index.html ) User.count will also take an where condition: @last_3_days = User.count [''created_at >= ?'', 3.days.ago] <%= @last_3_days %> members have signed up in the last 3 days It''s a good idea to check out all the ActiveRecord methods. If you''re not sure how to properly use them, play with them using script/console.
i tried that and it works but my setup isnt working.
I have a main layout called "main_layout".
inside the layout is a line like this:
<div id="bottominfo">There are <%= render_partial
"/content/
totalmembers/" %></div>
then in my controller ContentController is the following:
def totalmembers
@count = User.count
end
Then in my partial /views/content/_totalmembers.rhtml i have
There are currently <%= @count %> users
However its still not printing out the total, just the text.. What am
i doing wrong here ?
thanks
adam
On Aug 19, 2005, at 4:49 PM, Nicholas Seckar wrote:
> Adam Denenberg wrote:
>
>> I have the following in my controller
>> def totalmembers
>> @total = User.find_by_sql "SELECT COUNT(*) as count FROM users
"
>> end
>> now inside my template how do i access the field "count" from
the
>> query so i can print out the number of members ?
>> I tried everythign like <%= @total.count %> but nothing works.
>>
>
> Hi Adam,
>
> If you do
>
> def total_members
> @count = User.count # @count will be set to an integer, such as 10
> end
>
> Then your template can contain
>
> There are <%= @count %> members signed up.
>
> You might want to use the pluralize helper to use
''member'' when
> appropriate. (Look it up on http://rails.rubyonrails.com/
> fr_method_index.html )
>
> User.count will also take an where condition:
>
> @last_3_days = User.count [''created_at >= ?'',
3.days.ago]
>
> <%= @last_3_days %> members have signed up in the last 3 days
>
>
> It''s a good idea to check out all the ActiveRecord methods. If
> you''re not sure how to properly use them, play with them using
> script/console.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Adam Denenberg wrote> inside the layout is a line like this: > > <div id="bottominfo">There are <%= render_partial > "/content/totalmembers/" %></div> > > Then in my partial /views/content/_totalmembers.rhtml i have > > There are currently <%= @count %> users > > However its still not printing out the totalYou need to pass along the count as a variable to the partial. You have 2 options for this, either pass it in the locals: <%= render_partial "/content/totalmembers/", :locals=>{:count=>@count} %> Available as: <%= count %> Or as the partials object: <%= render_partial "/content/totalmembers/", :object=>@count %> Available as the name of the partial: <%= @totalmembers %> Hope that helps. -- R.Livsey http://livsey.org