如何让一个值属于另一个值?

How to Make a Value Belong to Another Value?

我想创建一个关系,当用户输入 :name 和 :result 时,该 :result 属于 :name,这样我最终可以创建一个 table,其中添加了每个新结果它添加到名称中。

我是在量化模型中创建这种关系,还是必须为名称和结果生成一个新模型?在此先感谢您的帮助!你可能会从这个问题中看出我是新手,我很难在网上找到答案。

class Quantified < ActiveRecord::Base
 belongs_to :user
  scope :averaged,  -> { where(categories: 'Monthly Average') }

 CATEGORIES = ['Monthly Average', 'One-Time Instance']

end

控制器

  def index
   @averaged_quantifieds = current_user.quantifieds.averaged
   @instance_quantifieds = current_user.quantifieds.instance
  end

索引

<!-- Default bootstrap panel contents -->

<div id="values" class="panel panel-default">
  
  <div class="panel-heading"><h4><b>AVERAGE</b></h4></div>

  <!-- Table -->
<table>
  <thead>
    <% @averaged_quantifieds.each do |averaged| %>
      <% if averaged.user == current_user %>
        <tr>
  <td>
            <th class="value">
            <%= averaged.name %>
            (<%= averaged.metric %>)
            </th>
        </tr>
  </thead>
  <tbody>
      <tr>
        <th class="category">
        <%= averaged.date.strftime("%m-%d-%Y") %>
        </th>

        <th class="value">
        <%= averaged.result %>
        </th>
      </tr>
  </tbody>
</td>
    <% end %>
    <% end %>
 </table>
</div>

形式

<%= form_for(@quantified) do |f| %>
  <% if @quantified.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@quantified.errors.count, "error") %> prohibited this quantified from being saved:</h2>

      <ul>
      <% @quantified.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="america">
<form>
   <%= f.select :categories, Quantified::CATEGORIES %>

   <br>
   <br>

  <div class="form-group">
    <%= f.text_field :name, class: 'form-control', placeholder: 'Enter Name' %>
  </div>

  <div class="form-group">
    <%= f.text_field :result, class: 'form-control', placeholder: 'Enter Result' %>
  </div>

  <div class="form-group">
    <%= f.text_field :metric, class: 'form-control', placeholder: 'Enter Metric' %>
  </div>

    <div class="date-group">
      <label> Date: </label>
      <%= f.date_select :date, :order => [:month, :day, :year], class: 'date-select' %>
    </div>


<div class="america2">
  <%= button_tag(type: 'submit', class: "btn") do %>
  <span class="glyphicon glyphicon-plus"></span>
  <% end %>

  <%= link_to quantifieds_path, class: 'btn' do %>
  <span class="glyphicon glyphicon-chevron-left"></span>
  <% end %>

  <%= link_to @quantified, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
  <span class="glyphicon glyphicon-trash"></span>
  <% end %>
</div>

</form>
</div>
<% end %>

架构

  create_table "quantifieds", force: true do |t|
    t.string   "categories"
    t.string   "name"
    t.string   "metric"
    t.decimal  "result"
    t.date     "date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end
  
  add_index "quantifieds", ["categories"], name: "index_quantifieds_on_categories"
  add_index "quantifieds", ["user_id"], name: "index_quantifieds_on_user_id"

#Creating a result model which will hold users results value & Date(as you mentioned)

$ rails g model result result_value:decimal result_date:datetime user_id:integer
$ rake db:migrate     

 #app/model/user.rb
class User < ActiveRecord::Base
  has_many :results
end

 #app/model/result.rb 
 class Result < ActiveRecord::Base 
    belongs_to :user 
 end

现在在终端中试试这个:

 $ user = User.first 
 $ user.results.create(result_value: 5, result_date: Time.now)
 $  user.results.create(result_value: 15, result_date: 5.days.ago)
 $ user.results #this will list the results of the user.. 

接下来尝试从视图-> 控制器实现相同的功能。 希望对您有所帮助:)