On Jun 6, 5:38 pm, Jay Covington <[email protected]>
wrote:
> Frederick Cheung wrote:
> > On Jun 6, 5:05 pm, Jay Covington <[email protected]>
> > wrote:
> >> >> @statcount = table.count(:all, :conditions => statcount_query).to_s
> >> ("grocery") that contains: "title", "item1", "item2", "item3", "item4,
> >> "item5". It was built behind a scaffold. So the index.rhtml shows all of
> >> my lists. Ok, no problem there. Here is example output:
>
> > What I meant by that is what is the structure of your data (columns,
> > etables etc.)
>
> > Fred
>
> Here is the format of index.rhtml and database layout:
>
> <table>
>   <% @listhubs.in_groups_of(3, false) do |row_listhubs| %>
>   <tr>
>        <% for listhub in row_listhubs %>
>     <td><%= listhub.title  +
> (link_to 'edit', edit_listhub_path(listhub) + '<br>' + listhub.item1 +
> '<br>' + listhub.item2 + '<br>' + listhub.item3+ '<br>' + listhub.item4+
> '<br>' + listhub.item5+ '<br>' + listhub.item6 + '<br>' + listhub.item7
> + '<br>' + listhub.item8 + '<br>' + listhub.item9 + '<br>' +
> listhub.item10 %>
>
>    </td>
>
>  <% end %>
>
> </tr>
>
> <% end %>
>
> </table>
>
> ---------------------------------------------------------------------------
>
> class CreateListhubs < ActiveRecord::Migration
>   def self.up
>     create_table :listhubs do |t|
>            t.column :title, :string
>       t.column :item1, :string
>       t.column :item2, :string
>       t.column :item3, :string
>       t.column :item4, :string
>       t.column :item5, :string
>       t.column :item6, :string
>       t.column :item7, :string
>       t.column :item8, :string
>       t.column :item9, :string
>       t.column :item10, :string
>

ah, so a list having 3 apples is represented by 3 of the columns
having value 'apple' ? That's an unusual way of organising your data,
and doing that sort of counting in sql is a nightmare. I would do
things like this:

class Listhub
   def values
     [item1, item2, item3, item4, item5, item6, item7, item8, item9,
item10].compact
   end
end

then look at values. You can use group_by to group identical items, ie

values.group_by {|value| value}

will return values like {"apple"=>["apple", "apple", "apple"],
"banana"=>["banana"], "pear"=>["pear", "pear"]}

and then it is simple enough to use map on that to get counts for each
key, eg

values.group_by {|v| v}.map {|key, items|[key, items.length]}

#=> [["apple", 3], ["banana", 1], ["pear", 2]] (assuming that values
evaluated to ["apple", "pear", "apple", "apple", "banana", "pear"])

Fred


>     end
>   end
>
>   def self.down
>     drop_table :listhubs
>   end
> end
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to