Rails模型中的实例变量 [英] Instance Variables in a Rails Model

查看:104
本文介绍了Rails模型中的实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个变量意见,我想将其作为实例变量存储在模型中...我是对的,假设我需要为其添加列,否则将不断对其进行重新计算吗?

I have this variable opinions I want to store as an instance variable in my model... am I right in assuming I will need to add a column for it or else be re-calculating it constantly?

我的另一个问题是,存储到列变量而不是本地变量中的语法是什么?

My other question is what is the syntax to store into a column variable instead of just a local one?

感谢您的帮助,代码如下:

Thanks for the help, code below:

# == Schema Information
#
# Table name: simulations
#
#  id          :integer          not null, primary key
#  x_size      :integer
#  y_size      :integer
#  verdict     :string
#  arrangement :string
#  user_id     :integer
#

class Simulation < ActiveRecord::Base
    belongs_to :user

    serialize :arrangement, Array

    validates :user_id, presence: true
    validates :x_size, :y_size, presence: true, :numericality => {:only_integer => true}
    validates_numericality_of :x_size, :y_size, :greater_than => 0

    def self.keys
        [:soft, :hard, :none]
    end 

    def generate_arrangement    
        @opinions = Hash[ Simulation.keys.map { |key| [key, 0] } ]
        @arrangement = Array.new(y_size) { Array.new(x_size) }

        @arrangement.each_with_index do |row, y_index|
            row.each_with_index do |current, x_index|
                rand_opinion = Simulation.keys[rand(0..2)]
                @arrangement[y_index][x_index] = rand_opinion
                @opinions[rand_opinion] += 1
            end
        end
    end

    def verdict
        if @opinions[:hard] > @opinions[:soft]
          :hard
        elsif @opinions[:soft] > @opinions[:hard]
          :soft
        else
          :push
        end
    end 

    def state 
        @arrangement
    end

    def next
        new_arrangement = Array.new(@arrangement.size) { |array| array = Array.new(@arrangement.first.size) }
        @opinions = Hash[ Simulation.keys.map { |key| [key, 0] } ]

        @seating_arrangement.each_with_index do |array, y_index|
            array.each_with_index do |opinion, x_index|
                new_arrangement[y_index][x_index] = update_opinion_for x_index, y_index
                @opinions[new_arrangement[y_index][x_index]] += 1
            end
        end

        @arrangement = new_arrangement
    end

    private 

    def in_array_range?(x, y)
        ((x >= 0) and (y >= 0) and (x < @arrangement[0].size) and (y < @arrangement.size))
    end

    def update_opinion_for(x, y)
        local_opinions = Hash[ Simulation.keys.map { |key| [key, 0] } ]

        for y_pos in (y-1)..(y+1)
            for x_pos in (x-1)..(x+1)
                if in_array_range? x_pos, y_pos and not(x == x_pos and y == y_pos)
                    local_opinions[@arrangement[y_pos][x_pos]] += 1
                end
            end
        end

        opinion = @arrangement[y][x]
        opinionated_neighbours_count = local_opinions[:hard] + local_opinions[:soft]

        if (opinion != :none) and (opinionated_neighbours_count < 2 or opinionated_neighbours_count > 3)
            opinion = :none    
        elsif opinion == :none and opinionated_neighbours_count == 3
            if local_opinions[:hard] > local_opinions[:soft]
                opinion = :hard
            elsif local_opinions[:soft] > local_opinions[:hard]
                opinion = :soft
            end 
        end 

        opinion
    end

end

推荐答案

ActiveRecord分析数据库表并通过元编程创建setter和getter方法.

ActiveRecord analyzes the database tables and creates setter and getter methods with metaprogramming.

因此,您将创建一个带有迁移的数据库列:

So you would create a database column with a migration:

rails g migration AddOpinionToSimulation opinion:hash

请注意,并非所有数据库都支持在列中存储哈希或类似的键/值数据类型. Postgres做到了.如果您需要使用另一个数据库(例如MySQL),则应考虑使用一种关系(将数据存储在另一个表中).

Note that not all databases support storing a hash or a similar key/value data type in a column. Postgres does. If you need to use another database such MySQL you should consider using a relation instead (storing the data in another table).

然后,当您访问simulation.opinion时,它将自动获取数据库列的值(如果记录是持久的).

Then when you access simulation.opinion it will automatically get the database column value (if the record is persisted).

由于ActiveRecord创建了一个setter和getter,因此您可以通过以下方式从模型内访问属性:

Since ActiveRecord creates a setter and getter you can access your property from within the Model as:

class Simulation < ActiveRecord::Base
  # ...

  def an_example_method
    self.opinions # getter method
    # since self is the implied receiver you can simply do
    opinions
    opinions = {foo: "bar"} # setter method.
  end
end 

使用普通红宝石attr_accessorattr_readerattr_writer宏时也是如此.

The same applies when using the plain ruby attr_accessor, attr_reader and attr_writer macros.

当您分配由数据库列支持的属性时,ActiveRecord将该属性标记为 dirty ,并在保存记录时将其包括在内.

When you assign to an attribute backed by a database column ActiveRecord marks the attribute as dirty and will include it when you save the record.

ActiveRecord有一些直接更新属性的方法:updateupdate_attributesupdate_attribute.呼叫签名及其处理回调的方式有所不同.

ActiveRecord has a few methods to directly update attributes: update, update_attributes and update_attribute. There are differences in the call signature and how they handle callbacks.

这篇关于Rails模型中的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆