I suspect something like this could work--may be worth a shot.

# Models
class Table < AR:Base
  has_many :column_groups, :order => 'cg_num'
  has_many :rows, :order => 'rownum'
  def columns
    self.column_groups.map {|cg| cg.columns}
  end
end 

class ColumnGroup < AR:Base
  belongs_to :table
  has_many :columns, :order => 'col_num'
end

class Column < AR:Base
  belongs_to :column_group
  has_many :cells, :order => 'col_num'
end

class Row < AR:Base
  belongs_to :table
  has_many :cells, :order = 'col_num'

  def get_cell(col)
    # there's almost certainly a faster 
    # way to do this, but this is all that 
    # comes to mind.
    cells.each do |cell|
      return cell if cell.column == col
    end
    return "&nbsp;"
  end

end

class Cell < AR:Base
  belongs_to :row
  belongs_to :column
end

# Controller
class TableController < ApplicationController
  def show
    # :include may be useful here if perf is an issue
    @table = Table.find_by_name(params[:name])
  end
end

# View
<table>
  <tr class = "column-group-head">
    <% @table.column_groups.each do |colgroup| %>
      <td colspan = <%= colgroup.columns.length%>><%= h colgroup.name %></td>
    <% end # colgroup %>
  </tr>
  <tr class = "column-head">
    <% @table.columns.each do |col| %>
      <td><%= h col.name %></td>
    <% end # col %>
  </tr>
  <% @table.rows.each do |row| %>
    <tr class = "data-rows">
      <% row.table.columns.each do |col| %>
        <td>
          <%= h row.get_cell(col) %>
        </td>
      <% end # cell %>
    </tr>
  <% end # row %>
</table>

- Roy

-----Original Message-----
From: rubyonrails-talk@googlegroups.com 
[mailto:rubyonrails-t...@googlegroups.com] On Behalf Of frankjmat...@gmail.com
Sent: Tuesday, February 10, 2009 7:36 AM
To: Ruby on Rails: Talk
Subject: [Rails] Re: sparse tables seems to be an impossible term to search on 
google


my bottleneck at the moment is "how do i properly merge all this quasi- normal 
data into an array FROM a rails find"... i can find all the sheets.. i can 
"find" all the sheets columns, i can find all the columns data... but i cant 
merge them into one cohesive structure which can then be easily iterated over 
ie:

please take this for the psudocode that it is.

--table--
  --thead--
    --tr--
      columngroups.each do |columngroup|
      --th colspan=columngroup.column_count--
        columngroup.name
      --/th--
      end
    --/tr--
    --tr--
      columns.each do |column|
      --th--
        column.name
      --/th--
      end
    --/tr--
  --/thead--
etc...

my main problem is that the find returns the columns, columngroups etc... are 
all in random order... i think i need them returned as a key/value so i can 
align everything properly.. but i dont know.

a sheet is just a way of giving all the columns/rows something to say "hey, 
this is my parent object"...

On Feb 10, 10:04 am, Randy Kramer <rhkra...@gmail.com> wrote:
> (top posting and not snipping very much so that somebody else can pick 
> up the thread)--I think I'm out of my depth trying to deal with the 
> best (i.e., fastest) way to deal with your data in Ruby/Rails.  (If I 
> was to hazard a guess, I might try dealing with each table as an 
> array, just for the sake of speed.)
>
> OTOH, if this is a rails application, presumably there is a client and 
> a server (and TCP/IP communication between them)--is the database 
> processing on the server really the bottleneck?
>
> At least one more comment interspersed below.
>
> On Tuesday 10 February 2009 09:37 am, frankjmat...@gmail.com wrote:
>
>
>
>
>
> > ive toyed around with just using txt files but my limited 
> > understanding of "proper technique" in dealing with them makes them 
> > just as cumbersome...
>
> > im very familiar with normalization and if it was practical (and the 
> > cost didnt outweigh the benefit) id make sure everything was 
> > absolutely 6NF and then some... but coulda, woulda, shoulda... its 
> > not practical.. the best im shooting for is 3NF or 4NF but its not a 
> > stringent requirement...
>
> > i guess you could say i know my way around databases, im just lost 
> > with trying to implement this in a ruby way. my database breakdown 
> > will probably look as follows (i think, unless someone can point me 
> > in a better direction)...
>
> > over time there may be 5000 sheets... each sheet may have up to 20 
> > columns. each column will eventually belong to exactly one group. 
> > each group may have up to 400 "rows"... .. so if a sheet has 4 
> > columns and
> > 2 groups like my prev. example and is filled to capacity... theres 
> > going to be 400 rows for each set of groups... 800 rows... they need 
> > to then be translated into one cohesive unit for display. the final 
> > display will have all 4 columns separated into groups and "merged" 
> > so all the "toolnumbers" line up in rows.. displaying only 400 rows.
>
> Without a lot more thinking, I don't fully follow the above 
> description. I guess "sheet" is the first thing that puzzles me--is a 
> sheet a table, or is there a table containing up to 5000 sheets?  (Maybe the 
> "schema"
> you list below would answer that and my other questions, but it would 
> probably take me a while to puzzle it out--more time than I have atm.)
>
> If I really wanted to understand it, I'd ask for an example using "real"
> data--sheets, columns, and rows just confuse me (would that be 
> metadata?).
>
>
>
>
>
> > i **think** i understand the database side.. im lost on the ruby 
> > implementation (or any implementation).. is there a "most effective"
> > way to construct my relationships?
>
> > Sheets
> > - id (int)
> > - name (string)
>
> > Columns
> > - id (int)
> > - sheet_id (int)
> > - column_group_id (int)
> > - name (string)
>
> > ColumnGroups
> > - id (int)
> > - name (string)
>
> > Data
> > - id (int)
> > - sheet_id (int)
> > - column_id (int)
> > - tool_number (string)
> > - value (int)
>
> > then i'll have a possible array as such for a query like:
> > select tool_number, value from data where sheet_id = x
>
> > whats an effective way to iterate over the returned dataset and sort 
> > it out into its corresponding columns column groups and rows... im 
> > seeing a join in my head but i dont know on what.
>
> > :(
>
> > hopefully my problem is becoming a little more clear... but the 
> > deeper i dig the more i suspect theres an elegent solution im not 
> > advanced enough to see.
>
> Good luck!
> Randy Kramer
> --
> I didn't have time to write a short letter, so I created a video 
> instead.--with apologies to Cicero, et.al.- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to