我应该将模型计算保存为属性吗?

时间:2015-02-14 13:47:31

标签: ruby-on-rails ruby design-patterns model-view-controller model

我有一个应用程序,允许用户使用表单创建一个新房间,以输入名称,描述,长度和宽度。创建的每个房间都成为新记录,应用程序会计算出“' size'房间的长度' *'宽度'。这是一个简单的应用程序,我正在玩这个学习Rails,但我可以更进一步,将一系列房间组成一个房子,一些总的大小'每个房子 我的问题涉及'尺寸'价值以及如何将其整合到应用中。我最初认为用户应该看到' size'在表单上,​​但一旦看起来可能需要Ajax,就搁置了。我移动了'#size;'尺寸'从视图到模型的方法计算符合"胖模型,瘦模控制器"概念,我现在展示'尺寸'在'指数'观点,留下新的'纯粹用于输入数据。

我最初设置的模型包括长度,宽度和大小。请参阅房间模型的迁移:

20150118183743_create_rooms.rb

class CreateRooms < ActiveRecord::Migration
  def change
    create_table :rooms do |t|
      t.string :name
      t.text :description
      t.integer :length
      t.integer :width
      t.integer :size

      t.timestamps null: false
    end
  end
end

我应该保存&#39;尺寸&#39;对于数据库的每条记录?我已经读过,将计算作为属性保存到模型中是不必要的。据推测,应用程序应该处理?思考这个问题的正确方法是什么?
我的索引&#39;查看计算&amp;返回最大长度&#39;和&#39; width&#39;,但是当我尝试计算最大尺寸时,我遇到了错误。我在模型中有一个计算(即方法),但它似乎是错误的。有什么建议? 以下是相关代码:

room.rb

class Room < ActiveRecord::Base
    validates :name,    presence: true, length: { maximum: 30 },
                                        uniqueness: { case_sensitive: false }
    validates :length, :width,  presence: true,
                                numericality: { only_integer: true, 
                                less_than_or_equal_to: 1000,
                                greater_than_or_equal_to: 1 }

    def size
        size = length * width
    end

    def max_room
        size.max
    end

end

rooms_controller.rb

class RoomsController < ApplicationController

  def show
    @room = Room.find(params[:id])
  end

  def new
    @room = Room.new
  end

    def index
    @rooms = Room.all
  end

  def create
        @room = Room.new(user_params)  
        if @room.save                   #a boolean, if able to save the instance
            flash[:success] = "You created a new Room!!"
            redirect_to @room   #we send the user to the room
        else
            render 'new'            #so we want to render the new template 
        end 
  end

    private
        def user_params
            params.require(:room).permit(:name, :description, :length,
                                         :width, :size)
        end


end

index.html.erb

<% provide(:title, 'All Rooms') %>

<h1>All rooms</h1>


<div class="container">
    <div class="row column-md-7">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Description</th>
                    <th class="text-right">Length (ft.) </th>
                    <th class="text-right">Width (ft.) </th>
                    <th class="text-right">Size (sq.ft.) </th>
                    <th class="text-center">Delete? </th>
                </tr>
            </thead>

            <tbody>

              <% @rooms.each do |room| %>
                <tr>
                  <td> <%= link_to room.name, room %> </td>
                  <td> <%= room.description %> </td>
                  <td class="text-right"> <%= room.length %> </td>
                  <td class="text-right"> <%= room.width %> </td>
                  <td class="text-right"> <%= room.size %> </td>
                  <td class="text-center"> <%= link_to "delete", room, method: :delete, 
                        data: { confirm: "You sure?" } %> </td>
                </tr>
              <% end %>
            </tbody>
        </table>

        <div class="alert alert-info">
      The model contains <%= pluralize(Room.count, "room") %> in total.  
      The max length is <%= Room.maximum('length') %>.
      The max width is <%= Room.maximum('width') %>.

    </div>

    </div>
</div>

我试着展示&#39;尺寸&#39;通过添加

The max size is <%= Room.max_room %>

但是返回了一个错误。

new.html.erb

<% provide(:title, "New Room")  %>
<h1>The Rooms page </h1>

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <%= form_for(@room) do |f| %>
            <%= render 'shared/error_messages' %>

            <%= f.label :name %>    
            <%= f.text_field :name %>

            <%= f.label :description %>
            <%= f.text_area :description %>

            <%= f.label :length, "Length (ft.)" %>
            <%= f.number_field :length %>

            <%= f.label :width, "Width (ft.)" %>
            <%= f.number_field :width %>

            <%= f.submit "Create my room", class: "btn btn-primary" %>

        <% end %>

    </div>
</div>

show.html.erb

<% provide(:title, @room.name)  %>
<h1>The "<%= @room.name %>" page </h1>
<h2>This page contains the show action associated with the 
    Rooms page </h2>
<br>
<br>

<div class="container">
    <div class="col-md-6 col-md-offset-3"> 
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Description</th>
                    <th class="text-right">Length (ft.) </th>
                    <th class="text-right">Width (ft.) </th>
                    <th class="text-right">Size (sq.ft.) </th>
                </tr>
            </thead>

            <tbody>
          <tr>
            <td> <%= @room.name %> </td>
            <td> <%= @room.description %> </td>
            <td class="text-right"> <%= @room.length %> </td>
            <td class="text-right"> <%= @room.width %> </td>
            <td class="text-right"> <%= @room.size %> </td>
          </tr>
        </tbody>

        </table>
    </div>
</div>

<hr>

<%= link_to "Create a new room", new_room_path, class: "btn btn btn-primary" %>

的routes.rb

Rails.application.routes.draw do

  root                  'static_pages#home'
  get 'home'            =>  'static_pages#home'
  get 'calculations'    => 'static_pages#calculations'
  get 'help'            =>  'static_pages#help'
  get 'about'           => 'static_pages#about'
  get 'new_room'        =>  'rooms#new'
  get 'rooms'           =>  'rooms#index'
  resources :rooms
end

我计划使用数字计算繁重的应用程序,因此我希望这些基础知识正确。我不希望应用程序的数据库爆炸,如果我在虚拟环境中完成(可能)完成太多的计算。

所以,回顾......

  1. 是否应将应用计算作为新记录的属性保存到数据库?
  2. 对于&#39; size&#39;,什么是正确的计算/方法?
  3. 如果我想对计算值执行计算,首先必须将该值保存为属性吗?

1 个答案:

答案 0 :(得分:2)

max_room的实现是错误的,因为size值只是一个数字,max方法没有在数字上定义,而是应该在Enumerable上调用价值观 所以Room应该以这种方式实现:

class Room < ActiveRecord::Base
    validates :name,    presence: true, length: { maximum: 30 },
                                        uniqueness: { case_sensitive:    false }
    validates :length, :width,  presence: true,
                                numericality: { only_integer: true, 
                                less_than_or_equal_to: 1000,
                                greater_than_or_equal_to: 1 }

    def size
        size = length * width
    end

    class << self
      # This is a class method, since it depends on all the rooms
      # not on a specific room 
      def max_size
          # This will delegate the calculation to the database
          select('MAX(length * width) AS max')[0]['max'];
      end
      # But, this will instantiate the records on memory before it makes the calculation
      # def max_room
      #    all.max{ |room| room.length * room.width }
      # end
      # This is a class method as well
      def max_room
        order('size DESC').first
      end
    end
end
  
      
  1. 是否应将应用计算作为新记录的属性保存到数据库?
  2.   

如果计算值所依赖的属性会频繁更改,在这种情况下,您不应保存计算值,而是在每次需要时计算它。但是我可以看到房间的长度和宽度不会改变,所以计算的值需要计算一次,并保存以便在需要时使用(例如计算max_size),所以在这种情况下你需要创建一个属性size并在使用钩子创建记录时对其进行计算。

before_save :calculate_size

private
def calculate_size
   size = length * width
end