如何在ActiveRecord中设置默认值?
我看到Pratik的一篇文章描述了一段丑陋,复杂的代码:http://m.onkey.org/2007/7/24/how-to-set-default-values-in-your-model
class Item < ActiveRecord::Base
def initialize_with_defaults(attrs = nil, &block)
initialize_without_defaults(attrs) do
setter = lambda { |key, value| self.send("#{key.to_s}=", value) unless
!attrs.nil? && attrs.keys.map(&:to_s).include?(key.to_s) }
setter.call('scheduler_type', 'hotseat')
yield self if block_given?
end
end
alias_method_chain :initialize, :defaults
end
Run Code Online (Sandbox Code Playgroud)
我已经看到以下示例谷歌搜索:
def initialize
super
self.status = ACTIVE unless self.status
end
Run Code Online (Sandbox Code Playgroud)
和
def after_initialize
return unless new_record?
self.status = ACTIVE
end
Run Code Online (Sandbox Code Playgroud)
我也看到人们把它放在他们的迁移中,但我宁愿看到它在模型代码中定义.
是否有规范方法为ActiveRecord模型中的字段设置默认值?
我有问题,我在Rails中有一个迁移设置列的默认设置,如下例所示:
def self.up
add_column :column_name, :bought_at, :datetime, :default => Time.now
end
Run Code Online (Sandbox Code Playgroud)
假设,我想在以后的迁移中删除默认设置,如何使用rails迁移?
我目前的解决方法是在rails迁移中执行自定义sql命令,如下所示:
def self.up
execute 'alter table column_name alter bought_at drop default'
end
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这种方法,因为我现在依赖于底层数据库如何解释这个命令.如果数据库发生更改,此查询可能不再起作用,迁移将被破坏.那么,有没有办法表示在rails中删除列的默认设置?
我正在使用rails,我想这样做attr_accessor :politics,默认设置为false.
有谁知道如何做到这一点,能够用简单的术语解释它吗?
鉴于以下内容schema.rb:
create_table "people", force: true do |t|
t.string "name", null: false
t.integer "age"
t.integer "height"
t.string "email"
t.boolean "married", default: false
t.text "bio"
t.integer "fav_number"
t.decimal "lucky_num", precision: 2, scale: 2
t.datetime "birthday"
t.datetime "created_at"
t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)
我想删除name默认值null: false.我已尝试使用单独的迁移change_column_default,但这对其没有影响schema.rb.有什么建议?
在我的整个申请self.中,没有必要引用用户的名字.name工作良好.
为什么以下代码需要self按预期工作?
class User< ActiveRecord::Base
before_save :validate_name
def validate_name
if self.name.nil? || self.name.empty?
self.name= "Mr. No Name"
end
end
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我知道validates_presence_of可以用来阻止保存,但如果没有给出名字,我想保存默认值.
Rails 3.0.7.
我正在Rails3和Formtastic中构建一个表单.我有以下字段:
<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"} %>
Run Code Online (Sandbox Code Playgroud)
其中生成的HTML类似于:
<input id="post_one" name="post" type="radio" value="one" />Awesome</label>
<input id="post_two" name="post" type="radio" value="two" />Great</label>
<input id="post_three" name="post" type="radio" value="three" /> Nice</label>
Run Code Online (Sandbox Code Playgroud)
那工作完美无瑕!
现在,我想知道如何传递一个将"Great"标记为默认(选定)值的选项.我尝试了以下操作,但我无法让它工作.
<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"}, :default => "one" %>
Run Code Online (Sandbox Code Playgroud)
我也试过传入:selected而:checked不是:default唉,它不起作用.
有人知道这样做的方法吗?
谢谢!
编辑: Aditya提出了一个非常好的观点.一些搜索产生了这个有用的提示.
我一直试图集体思考如何计算用户响应率(对收到的消息的响应百分比)和时间(用户响应消息的速度).此信息将显示在用户个人资料中.我想不出一个适用于此的公式,并且没有看到任何其他关于它的帖子.
例如,用户A的响应率为85%,响应时间为3天.攻击它的正确方法是什么?
消息模型:
attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
validates_presence_of :subject, :message => "Please enter message title"
has_many :notifications, as: :event
scope :unread, -> {where('read_at IS NULL')}
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'
# Based on if a message has been read by it's recipient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recipient_id => user.id)
end
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end …Run Code Online (Sandbox Code Playgroud) 在Rails中为模型设置默认值的正确方法是什么?
class User < ActiveRecord::Base
attr_accessible :name, :points
end
Run Code Online (Sandbox Code Playgroud)
我希望积分从0开始而不是nil.理想情况下,默认值是立即创建的,而不是等待User保存到数据库中.但我想使用before_save或数据库约束也可以工作:
class User < ActiveRecord::Base
attr_accessible :name, :points
before_save :set_defaults
private
def set_defaults
self.points = 0
end
end
Run Code Online (Sandbox Code Playgroud)
使用最新的稳定Rails.
activerecord ruby-on-rails activemodel ruby-on-rails-3 ruby-on-rails-3.2