如何使价值属于另一种价值?

时间:2015-01-09 20:12:34

标签: ruby-on-rails model relationship

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

我是否在量化模型中创建此关系,或者是否必须为Name&amp ;;生成新模型结果?在此先感谢您的帮助!正如你可以告诉我的问题我是新手,我很难在网上找到答案。



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

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

end
&#13;
&#13;
&#13;

控制器

&#13;
&#13;
  def index
   @averaged_quantifieds = current_user.quantifieds.averaged
   @instance_quantifieds = current_user.quantifieds.instance
  end
&#13;
&#13;
&#13;

索引

&#13;
&#13;
<!-- 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>
&#13;
&#13;
&#13;

形式

&#13;
&#13;
<%= 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 %>
&#13;
&#13;
&#13;

模式

&#13;
&#13;
  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"
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

#Creating一个结果模型,它将保存用户的结果值&amp;日期(如您所述)

$ 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.. 

接下来尝试从view-&gt;实现相同的功能。控制器。 希望它有帮助:)