问题
Rows & columns, baby. Rows & columns.
Within one table:
When a User adds a new :name & :metric I want it to create a new column.
When a User adds a new :result_value & :date_value I want it to create a new row.

This is what my table currently looks like:

For the life of me I can't figure out why something that seems so elementary is so difficult. I don't know if I'm suppose to create the rows and columns in the database, model, helper or index view and/or if I should be using some other language or gem to help me. Any direction would be greatly appreciated because a dozen google searches hasn't done it for me.
Do I use add_column
in the database or something?
class CreateQuantifieds < ActiveRecord::Migration
def change
create_table :quantifieds do |t|
t.string :categories
t.string :name
t.string :metric
t.timestamps null: false
end
end
end
Or do I add columns via the model?
class Quantified < ActiveRecord::Base
belongs_to :user
has_many :results #correct
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true #correct
scope :averaged, -> { where(categories: 'averaged') }
scope :instance, -> { where(categories: 'instance') }
CATEGORIES = ['averaged', 'instance']
end
class Result < ActiveRecord::Base
belongs_to :user
belongs_to :quantified
end
Or do I keep playing around with the index view I've tried all sorts of 's and html tags, but I could never get the code to look like above, which is why I think the answer may lie deeper.
<!-- Default bootstrap panel contents -->
<div id="values" class="panel panel-default">
<div class="panel-heading"><h4><b>AVERAGE</b></h4></div>
<table>
<% @averaged_quantifieds.each do |averaged| %>
<% if averaged.user == current_user %>
<th><%= averaged.name %> (<%= averaged.metric %>)</th>
<td><% averaged.results.each do |result| %>
<tr><td><%= result.date_value.strftime("%m-%Y") %></td>
<td><%= result.result_value %></td></tr>
<% end %></td>
<% end %>
<% end %>
</table>
</div>
<br>
<br>
<br>
<br>
<!-- Default bootstrap panel contents -->
<div id="values" class="panel panel-default">
<div class="panel-heading"><h4><b>INSTANCE</b></h4></div>
<table>
<% @instance_quantifieds.each do |instance| %>
<% if instance.user == current_user %>
<th><%= instance.name %> (<%= instance.metric %>)</th>
<td><% instance.results.each do |instance| %>
<tr><td><%= result.date_value.strftime("%m-%Y") %></td>
<td><%= result.result_value %></td></tr>
<% end %></td>
<% end %>
<% end %>
</table>
</div>
<div class="values-button">
<%= link_to new_quantified_path, class: 'btn' do %>
<b><span class="glyphicon glyphicon-plus"</span></b>
<% end %>
</div>
回答1:
I think you may have a misunderstanding of how the database should be used.
I don't think you would ever want the actual database tables to be dynamic. Instead, you should set up two tables. One for the User, and one for the Metrics. Then a user can add new metrics.
# == Schema Information
# Table name: users
# id :integer
Class User < ActiveRecord::Base
has_many :metrics
end
# == Schema Information
# Table name: metrics
# id :integer
# type :string
# result :string
# user_id :integer
Class Metric < ActiveRecord::Base
belongs_to :user
end
This way, you can create as many types of metrics as you want. And you can determine who the metric belongs to by the :user_id field.
Reading this from head to toe should help: http://guides.rubyonrails.org/association_basics.html
来源:https://stackoverflow.com/questions/28242403/create-a-dynamic-table-attributes-columns-nested-attributes-rows