# Undefined method \`goals\_against' for #\<YAML::Object:0x5584864\>

**URL:** <https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029>\
**Category:** Rails\
**Created:** [April 17, 2008, 10:25am UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029 "2008-04-17T10:25:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 17, 2008, 10:25am UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/1 "2008-04-17T10:25:25Z")

</div>

Hi all,

I have a serialized object (of class Goals) in my statistics table. I  
store it in the value row of statitics  
When I do:  
goals = Statistic.find(1).value.goals\_against I get  
undefined method `goals\_against’ for #YAML::Object:0x5584864  
(goals\_against is a member of Goals)

Someone knows why this happens?

Thanks  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 17, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/2 "2008-04-17T16:01:16Z")

</div>

Would you share your model definitions and the code that you use to  
create/save the statistic and goals instances?

Craig

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 17, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/3 "2008-04-17T16:52:42Z")

</div>

Thanks for the reply,  
Please find the models, database and class definition below.

I try to get the goals object like this  
@statistics = @event.statistics.find(:all, :include  
=\> :statistic\_definition)  
goals\_a = @statistics.first.value.goals\_against  
=\> undefined method `goals\_against’ for #YAML::Object:0x579cbec

The YAMLvalue in the database is:  
— !ruby/object:Goals  
goals:  
1: 2  
goals\_against: 2  
goals\_for: 2  
version: 1

The model:  
class Statistic \< ActiveRecord::Base  
belongs\_to :event  
belongs\_to :statistic\_definition  
serialize :value  
end

statitics table  
id int(11)  
value text  
event\_id int(11)  
statistic\_definition\_id int(11)

The object I serialize  
class Goals  
attr\_accessor :goals  
attr\_accessor :goals\_for  
attr\_accessor :goals\_against  
attr\_accessor :penalty\_for  
attr\_accessor :penalty\_against

def initialize(goals, goals\_against, penalty\_for = nil,  
penalty\_against = nil)  
@version = 1  
@goals = goals  
set\_goals\_for()  
@goals\_against = goals\_against.to\_i  
unless penalty\_for.nil?  
@penalty\_for = penalty\_for.to\_i  
@penalty\_against = penalty\_against.to\_i  
end  
end

private  
def set\_goals\_for  
@goals\_for = 0  
@goals.each\_value {|value| @goals\_for += value.to\_i }  
end  
end

On Apr 17, 4:00 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 17, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/4 "2008-04-17T19:36:53Z")

</div>

I tried something a bit simpler just to see if it would work. Here’s  
what I  
did.

- create a new rails app (rails blah)
- create the databases (rake db:create:all)
- generate a Statistic model with a value column of type text  
(script/generate model statistic value:text)
- run migrations (rake db:migrate)
- create a non-ActiveRecord class named Goals with one attributed named  
goals\_against
- run the Rails console (script/console) and do the following:

> > Statistic.create! :value =\> Goals.new(3)  
> > =\> #\<Statistic id: 2, value: #\<Goals:0x1928960 @goals\_against=3\>,  
> > created\_at: “2008-04-17 13:22:10”, updated\_at: “2008-04-17 13:22:10”\>
> 
> > stat = Statistic.find :first  
> > =\> #\<Statistic id: 2, value: #\<Goals:0x1911ddc @goals\_against=3\>,  
> > created\_at: “2008-04-17 13:22:10”, updated\_at: “2008-04-17 13:22:10”\>
> 
> > stat.value.goals\_against  
> > =\> 3

I didn’t have any problems along the way. Other than using simpler  
models/classes, nothing jumps out at me as to what you might have done  
that’s causing your problem.

Here’s the source code.

class Statistic \< ActiveRecord::Base  
serialize :value  
end

class Goals  
attr\_accessor :goals\_against

def initialize(goals\_against)  
@goals\_against = goals\_against  
end  
end

Here’s the generated schema from db/schema.rb.

ActiveRecord::Schema.define(:version =\> 1) do

create\_table “statistics”, :force =\> true do |t|  
t.text “value”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

end

Let me know how it goes and if there’s anything more I can do.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 18, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/5 "2008-04-18T17:47:50Z")

</div>

Many thanks for the help.

I have found some strange behavior:

I use understanding code to first insert a then read a Goals object  
from the database  
stat = Statistic.new  
stat.event\_id = 1  
stat.statistic\_definition\_id = 1  
stat.value = Goals.new({1 =\> 1, 2 =\> 2}, 1)  
stat.save

@statistics = @event.statistics.find(:all, :include  
=\> :statistic\_definition)  
goals\_a = @statistics.first.value.goals\_against

When I use above code the deserialization works as it should.

The next time, when I only get the Goals object out of the database  
(and not insert it first), deserialization fails. It seems that I have  
to insert and read the object in the same request otherwise  
deserialization fails.

This sounds very weird and I have no idea what causes this.

Regards,  
Stijn

On Apr 17, 7:35 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 18, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/6 "2008-04-18T17:50:12Z")

</div>

I found out something more. When I do:  
Goals.new({1 =\> 1, 2 =\> 2}, 1)  
@statistics = @event.statistics.find(:all, :include  
=\> :statistic\_definition)  
goals\_a = @statistics.first.value.goals\_against

it works.  
@statistics = @event.statistics.find(:all, :include  
=\> :statistic\_definition)  
goals\_a = @statistics.first.value.goals\_against

I get undefined method `goals\_against’ for #YAML::Object:0x539a954

Can it be that the deserializer doesn’t know the Goals object somehow  
by default? I put the Goals class file in my Lib directory.

Regards,  
Stijn

When I do

it also works. I thus just have to use the Goals class

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 19, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/7 "2008-04-19T15:01:27Z")

</div>

In the example that I created, I put the Goals class in app/models even  
though it’s not an ActiveRecord model. Does putting your Goals class in  
app/models make a difference?

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 22, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/8 "2008-04-22T17:37:42Z")

</div>

Hi Craig,

Putting it in the apps/model solves the problem. I though tough you  
should put all your classes in the lib dir.

regards,  
Stijn

On Apr 19, 3:00 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 23, 2008, 1:40am UTC](https://www.ruby-forum.com/t/undefined-method-goals-against-for-yaml-0x5584864/135029/9 "2008-04-23T01:40:34Z")

</div>

I’ve used the lib dir only once for a very simple rake task, but my  
impression is that code in the lib dir should be available to the rest  
of  
the app. If I have a chance to do a little learning, I’ll report back.

Craig
