在 rails 模型类中使用 attr_accessor

cur*_*ser 5 ruby ruby-on-rails

请原谅我的菜鸟问题。我是 ruby​​ 和 rails 的新手,需要一些帮助来理解我在代码中遇到的 attr_accessor

  create_table "pets", force: :cascade do |t|
            t.string   "name"
            t.string   "colour"
            t.string   "owner_name"
            t.text     "identifying_characteristics"
            t.text     "special_instructions"
            t.datetime "created_at"
            t.datetime "updated_at"
            t.string   "email"
            t.string   "password"
          end
Run Code Online (Sandbox Code Playgroud)

模型

 class Pet < ActiveRecord::Base
  has_many :pet_photos
  cattr_accessor :form_steps do
    %w(identity characteristics instructions)
  end

  attr_accessor :form_step
  validates :email, presence: true
  validates :name, :owner_name, presence: true, if: -> { required_for_step?(:identity) }
  validates :identifying_characteristics, :colour, presence: true, if: -> { required_for_step?(:characteristics) }
  validates :special_instructions, presence: true, if: -> { required_for_step?(:instructions) }

  def required_for_step?(step)
    return true if form_step.nil?
    return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
  end



end
Run Code Online (Sandbox Code Playgroud)

      class Pet::StepsController < ApplicationController
        include Wicked::Wizard
        steps *Pet.form_steps

        def show
          @pet = Pet.find(params[:pet_id])
          render_wizard
        end

        def update
          @pet = Pet.find(params[:pet_id])
          @pet.update(pet_params(step))

            if params[:images]

                params[:images].each do |image|
                @pet.pet_photos.create(image: image)
              end
            end

          render_wizard @pet
        end

        private

        def pet_params(step)
          permitted_attributes = case step
                                 when "identity"
                                   [:name, :owner_name]
                                 when "characteristics"
                                   [:colour, :identifying_characteristics]
                                 when "instructions"
                                   [:special_instructions]
                                 end

          params.require(:pet).permit(permitted_attributes).merge(form_step: step)
        end

      end
Run Code Online (Sandbox Code Playgroud)

路线

  PetThing::Application.routes.draw do
          resources :pets, only: [:new, :create, :index, :destroy] do
            resources :steps, only: [:show, :update], controller: 'pet/steps'
          end

          root to: 'pets#index'
Run Code Online (Sandbox Code Playgroud)

1)我只知道 attr_accessor ,它只不过是一个对象的getter/setter。我没有一个form_step作为我的宠物模型(宠物表)的属性。但是在模型中,有attr_accessor :form_step.getter是什么/setter 生成为:form_step?

2):form_stepPet 类的对象吗?

我并不完全理解attr_accessorRails 模型类中方法的用法。

3)请向我解释一个场景,我必须为模型属性(宠物表属性或字段)生成getter/setter

例如:attr_accessor :name attr_accessor :colour..等

我们什么时候使用attr_accessor模型属性或字段?

Sat*_*ati 5

让我们从 OOP 开始,基本上class是定义其实例(即对象)的属性和行为的模板。

rails 模型也是一个 ruby​​ 类。其属性由 定义attr_accessor,行为由 定义static and instance methods

考虑一个类 with attr_accessor

class Animal
      //defines animal properties
      attr_accessor :legs
      //defines animal behavior
      def talk
      end
end
Run Code Online (Sandbox Code Playgroud)

without attr_accessor

class Animal
      //defines animal properties
      //getter
      def legs
        @legs
      end
      //setter
      def legs=(value)
        @legs = value
      end

      //defines animal behavior
      def talk
      end
end
Run Code Online (Sandbox Code Playgroud)

两种表示完全相同。该attr_accessor自动添加getter和setter的我们,使开发者的生活更轻松。

让我们回答你的问题

  1. attr_accessor如前所述,Ruby 会自动为您添加这些。

  2. form_step它是Pet class和 的一个属性,可以作为 访问petObj.form_step

  3. 如果表与模型关联/映射,那么您不必将表中的列定义为attr_accessor,Rails 会自动照顾您。如果表中不存在字段或属性,则需要手动将其定义为attr_accessorin Model

我希望这回答了你的问题。