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
当我进入控制台并创建新记录时,“已批准”属性设置为true,为什么?
如何将其自动设置为默认值(false),如我的迁移文件中所示?
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>
没有找到相关结果
已邀请:
2 个回复
稼悸
暂时跳过所有范围(包括
)。这应该允许较低级别的数据库默认机制生效*。
* ActiveRecord模糊了数据库(模式)中定义的默认值与应用程序(模型)中的默认值之间的差异。在初始化期间,它解析数据库模式并记录在那里指定的任何默认值。稍后,在创建对象时,它会分配这些模式指定的默认值,而不会触及数据库。例如,你会在
的结果中看到
(而不是
),即使数据从未被发送到数据库以使其填充其默认值(ActiveRecord根据它提取的信息抢先填写默认值超出架构)。
赐黄