views:

77

answers:

2

Using ruby, camping webframework, activerecord-2.1.1, my db structure is ...

create_table :Conf_posts do |t|
  %w{title body username posttime hit passwd}.each do |col|
    t.column :"#{col}", :string
    end
end

I want sum of each username's hit

I have the following code.

Post.find :all, :select => "username,sum(hit)", :from => "Conf_posts", :group => "username"

I received the following results. Only username was returned:

[#<Conf::Models::Post username: "\352\260\225\355\230\201">, #<Conf::Models::Post username: "\353\215\225\352\267\234">, #<Conf::Models::Post username: "\353\225\214\355\230\270">, #<Conf::Models::Post username: "\353\263\264\353\236\214">, #<Conf::Models::Post username: "\354\230\201\352\262\275">, #<Conf::Models::Post username: "\354\232\260\353\236\214">, #<Conf::Models::Post username: "\354\235\270\354\204\235">, #<Conf::Models::Post username: "\354\240\225\355\231\224.">, #<Conf::Models::Post username: "\355\230\201\354\235\264">]

I can't receive both username and sum of username'hit

+2  A: 

i'stupid :-)

answer is..

Post.sum "hit", :group => "username"

bye~

hillpath
+4  A: 
Post.sum(:hit, :group => :username)

should give an array of arrays, something like

[ ['user1', 42], ['user2', 109], ...
Mike Woodhouse