我有一个带有'属性'的表Radcheck作为列之一。使用safe_attribute gem我能够写入和读取列值,但在保存对象时,我得到以下错误。
Radcheck.new
x = Radcheck.new
x.send(:write_attribute, :attribute, 'aa')
x.send(:read_attribute, :attribute, 'aa')
x.save
保存对象时的错误记录
ArgumentError: wrong number of arguments (0 for 1)
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/attribute_methods/read.rb:141:in `attribute'
from /.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/dirty.rb:143:in `attribute_change'
from /.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/dirty.rb:117:in `block in changes'
from /.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/dirty.rb:117:in `map'
from /.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.3/lib/active_model/dirty.rb:117:in `changes'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/attribute_methods/dirty.rb:23:in `save'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block in save'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/transactions.rb:240:in `save'
from (irb):46
from /.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.3-p0 :047 >
答案 0 :(得分:0)
嗨朋友我认为没有与safe_attributes gem相关的问题。
Radcheck.new
x = Radcheck.new
x.send(:write_attribute, :attribute, 'aa')
#read_attribute does not requires any value
#x.send(:read_attribute, :attribute, 'aa')
x.send(:read_attribute, :attribute)
x.save
您也可以直接在活动记录对象上调用real_attribute,write_attribute。
Radcheck.new
x = Radcheck.new
x.write_attribute(:attribute, 'aa')
x.read_attribute(:attribute)
x.save
还要确保模型中有以下内容。
class Radcheck < ActiveRecord::Base
bad_attribute_names :attribute
end