# Creating a ruby class with mass assignment

**URL:** <https://www.ruby-forum.com/t/creating-a-ruby-class-with-mass-assignment/159016>\
**Category:** Ruby\
**Created:** [January 30, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/creating-a-ruby-class-with-mass-assignment/159016 "2009-01-30T13:10:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 30, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/creating-a-ruby-class-with-mass-assignment/159016/1 "2009-01-30T13:10:55Z")

</div>

Hi,  
How do I create a ruby class that has mass assignment functionality? I  
want it to work in a similar way to the way in which ActiveRecord does,  
ie.  
MyClass.new  
=\> #MyClass:  
MyClass.new(:name =\> “Cool class”)  
=\> #\<MyClass: @name=“s”\>  
MyClass.new(:name =\> “Cool class”, :other\_attribute =\> “some other”)  
=\> #\<MyClass: @name=“s”, @other\_attribute=“some other”\>

I have found the following code snippit which works for the mass  
assignment part but doesn’t work if I initialize the class without any  
arguments:

class MyClass  
attr\_accessor :name  
attr\_accessor :other\_attribute

def initialize(args)  
args.keys.each { |name| instance\_variable\_set “@” + name.to\_s,  
args[name] }  
end  
end

MyClass.new  
ArgumentError: wrong number of arguments (0 for 1)  
from (irb):19:in `initialize' from (irb):19:in`new’  
from (irb):19

What is the best way of achieving mass assignment whilst still allowing  
to initialize without parameters?

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 30, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/creating-a-ruby-class-with-mass-assignment/159016/2 "2009-01-30T13:46:33Z")

</div>

> What is the best way of achieving mass assignment whilst still allowing  
> to initialize without parameters?

def initialize(args = {})  
args.keys.each { … }  
end

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 30, 2009, 2:36pm UTC](https://www.ruby-forum.com/t/creating-a-ruby-class-with-mass-assignment/159016/3 "2009-01-30T14:36:34Z")

</div>

2009/1/30 Ruby N.y [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> args[name] }  
> to initialize without parameters?  
> You can make your life considerably easier by using Struct:

irb(main):001:0\> Test = Struct.new :foo, :bar, :baz do  
irb(main):002:1\* def self.create(h = {})  
irb(main):003:2\> new(\*members.map {|m| h[m.to\_sym]})  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> Test  
irb(main):006:0\> Test.create(:foo =\> 1)  
=\> #  
irb(main):007:0\> Test.create(:bar =\> 3, :foo =\> 123)  
=\> #

Kind regards

robert
