diff --git a/lib/sequel/model/exceptions.rb b/lib/sequel/model/exceptions.rb index 30e98edd9..72a5a98f5 100644 --- a/lib/sequel/model/exceptions.rb +++ b/lib/sequel/model/exceptions.rb @@ -31,9 +31,10 @@ class MassAssignmentRestriction < Error attr_reader :model def self.create(msg, model) - @model = model msg += " for class #{model.class.name}" if model.class.name - new(msg) + new(msg).tap do |err| + err.instance_variable_set(:@model, model) + end end end diff --git a/spec/model/base_spec.rb b/spec/model/base_spec.rb index 9eec636c1..811c9a991 100644 --- a/spec/model/base_spec.rb +++ b/spec/model/base_spec.rb @@ -683,6 +683,7 @@ def self.name; 'Item' end err = proc{item.new(:a=>1)}.must_raise(Sequel::MassAssignmentRestriction) err.message.must_equal("method a= doesn't exist for class Item") + err.model.class.must_equal(item) end it "should be disabled by strict_param_setting = false" do diff --git a/spec/model/exceptions_spec.rb b/spec/model/exceptions_spec.rb new file mode 100644 index 000000000..f6dd8c96d --- /dev/null +++ b/spec/model/exceptions_spec.rb @@ -0,0 +1,15 @@ +require_relative "spec_helper" + +describe Sequel::MassAssignmentRestriction, "#create" do + it "should sets model attr" do + model_cls = Class.new + model_cls.class_eval do + def self.name; 'TestModel' end + end + + model = model_cls.new + err = Sequel::MassAssignmentRestriction.create("method foo doesn't exist", model) + err.message.must_equal("method foo doesn't exist for class TestModel") + err.model.must_equal(model) + end +end