rails3 default_scope和迁移中的默认列值 [英] rails3 default_scope, and default column value in migration

查看:70
本文介绍了rails3 default_scope和迁移中的默认列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class CreateCrews < ActiveRecord::Migration
  def self.up
    create_table :crews do |t|
      t.string :title
      t.text :description
      t.boolean :adult
      t.boolean :private
      t.integer :gender_id
      t.boolean :approved, :default => false
      t.timestamps
    end
  end
  def self.down
    drop_table :crews
  end
end


class Crew < ActiveRecord::Base
  has_many :users, :through => :crew_users
  belongs_to :user

  default_scope where(:approved => true)
end

当我进入控制台并创建新记录时,"approved"属性设置为true,为什么?

When I go to console, and create a new record, the "approved" property is set to true, why ?

如何将其自动设置为迁移文件中所示的默认值(false)?

How I can set it automatically to the default value (false) like shown in my migration file ?

wojciech@vostro:~/work/ze$ rails console Loading development environment (Rails 3.0.0) ruby-1.9.2-p0 > c = Crew.new => #<Crew id: nil, title: nil, description: nil, adult: nil, private: nil, gender_id: nil, approved: true, created_at: nil, updated_at: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil>

wojciech@vostro:~/work/ze$ rails console Loading development environment (Rails 3.0.0) ruby-1.9.2-p0 > c = Crew.new => #<Crew id: nil, title: nil, description: nil, adult: nil, private: nil, gender_id: nil, approved: true, created_at: nil, updated_at: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil>

推荐答案

The documentation fordefault_scope says that the provided scope is applied to both queries and new objects. Default values supplied at the model level will always take precedence over defaults provided at the schema level because they are made inside the application before the data is ever sent to the database.

您可以使用unscoped暂时跳过所有作用域(包括default_scope).这应该使较低级别的数据库默认机制生效. * .

You can use unscoped to temporarily skip all scoping (including the default_scope). This should allow the lower level database defaulting mechanism to take effect*.

Crew.unscoped.new

* ActiveRecord掩盖了数据库中定义的默认设置(模式)与应用程序中的默认设置(模型)之间的差异.在初始化期间,它将解析数据库架构并记录在那里指定的任何默认值.稍后,当创建对象时,它会分配那些模式指定的默认值,而无需接触数据库.例如,即使数据从未发送到数据库以使其填充默认值(ActiveRecord抢先填充默认值),您也会在Crew.unscoped.new的结果中看到approved: false(而不是approved: nil).值基于它从架构中提取的信息).

*ActiveRecord obscures the difference between defaulting defined in the database (schema) and defaulting done in the application (model). During initialization, it parses the database schema and notes any default values specified there. Later, when creating objects, it assigns those schema-specified default values without touching the database. For example, you will see approved: false (instead of approved: nil) in the result of Crew.unscoped.new even though the data has never been sent to the database to get it to fill in its default value (ActiveRecord preemptively fills in the default value based on information it pulled out of the schema).

这篇关于rails3 default_scope和迁移中的默认列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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