# Setting attribute in constructor, .NEW works but not .CREATE

**URL:** <https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376>\
**Category:** Rails\
**Created:** [June 25, 2006, 12:59am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376 "2006-06-25T00:59:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 25, 2006, 12:59am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376/1 "2006-06-25T00:59:42Z")

</div>

I have table “decks” with three fields: “id”, “created\_at” and “cards”  
which is a 264-character string field. I have modified the model with a  
constructor, as follows:

class Deck \< ActiveRecord::Base  
attr\_reader :cards  
def initialize  
@cards = “12345”  
end  
end

If I call Deck.new from my controller, I get no errors and an object  
with the “cards” attribute set appropriately to “12345”. This object can  
be saved without error by calling the .save method. However, if I call  
Deck.create (in order to create and save in one fell swoop), then I get  
an error, shown below. If I take out the constructor method, Deck.create  
works just fine.

I’d appreciate any assistance.  
thanks.  
yb

ArgumentError in PokerController#index  
wrong number of arguments (1 for 0)  
RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace  
c:/server/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:406:in  
`initialize' c:/server/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:406:in`new’  
c:/server/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:406:in  
`create' #{RAILS_ROOT}/app/controllers/poker_controller.rb:3:in`index’

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 25, 2006, 1:07am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376/2 "2006-06-25T01:07:26Z")

</div>

try changing your initialize method…

def initialize( \*args )  
super  
@cards = “12345”  
end

This will allow Deck.new calls which have arguments, whiche active  
record  
expects all objects to allow.

mark

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 25, 2006, 1:31am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376/3 "2006-06-25T01:31:09Z")

</div>

Excellent! That is getting me past the error and getting my row into the  
database with a call to Deck.create.

However…

In the database row that is created, the “cards” column is blank. In the  
controller/view, after calling Deck.create, the “cards” attribute is  
populated with “12345” as expected, it’s just not making it into the db.

So - the database save is happening before my constructor populates the  
attribute (or something)?

???

Thanks so much for you help, Mark.

c.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 25, 2006, 3:52am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376/4 "2006-06-25T03:52:23Z")

</div>

The reason for this is because you are not using the cards attribute  
from  
the database. You have your class defined like so…

class Deck \< ActiveRecord::Base  
attr\_reader :cards  
def initialize( \*args )  
super  
@cards = “12345”  
end  
end

If the Deck table has a cards column, you should define your class like  
so…

class Deck \< ActiveRecord::Base  
def initialize( \*args )  
super  
self.cards = “12345”  
end  
end

Rails will automatically create the methods Deck#cards and Deck#cards=.  
When  
you do attr\_reader, you are overwriting the Deck#cards method to return  
the  
instance variable @cards. This never sets the actual variable which  
active  
record uses to store in the db.

hope that helps,

mark

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 25, 2006, 4:05am UTC](https://www.ruby-forum.com/t/setting-attribute-in-constructor-new-works-but-not-create/64376/5 "2006-06-25T04:05:36Z")

</div>

Like a charm!!!

Sorry - took a dinner break, put the kids to bed, etc. Just got back and  
implemented according to your help and everything is working perfectly.

Again - many thanks.  
c.
