# "undefined method"-error

**URL:** <https://www.ruby-forum.com/t/undefined-method-error/155968>\
**Category:** Rails\
**Created:** [December 18, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/undefined-method-error/155968 "2008-12-18T17:35:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_SSSS\_pke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecae2f/32.png) [@Daniel\_SSSS\_pke](https://www.ruby-forum.com/u/Daniel_SSSS_pke)\
**Post date:** [December 18, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/undefined-method-error/155968/1 "2008-12-18T17:35:12Z")

</div>

Hi,

I’m new to rails and so far think it’s great, but I’m having some  
major issues with a many-through relationship.

There are three tables:  
reporttemplates  
articlestemplates\_reporttemplates (additional attribute “optional”)  
articletemplates

They are connected to each other via has\_many and through + belongs\_to  
in articlestemplates\_reporttemplates.

Now, I need to display all articletemplates and show, if there is a  
connection (entry in articlestemplates\_reporttemplates) to the current  
reporttemplate. So in the controller I call  
@articletemplates = Articletemplate.find(:all, :joins=\>"LEFT JOIN  
articletemplates\_reporttemplates ON  
`articletemplates_reporttemplates`.articletemplate\_id =  
`articletemplates`.id WHERE  
`articletemplates_reporttemplates`.reporttemplate\_id = "+params[:id])

So far, everything is working. When I call  
\<% puts YAML::dump(articletemplate.articletemplates\_reporttemplates)  
%\>  
in the view, it gives me:

- !ruby/object:ArticletemplatesReporttemplate  
attributes:  
created\_at: 2008-12-16 17:24:19  
optional: “0”  
updated\_at: 2008-12-16 17:24:19  
id: “1”  
reporttemplate\_id: “1”  
articletemplate\_id: “1”  
attributes\_cache: {}

But when I try to access the attribute “optional”, I always get an  
error message!  
\<% if articletemplate.articletemplates\_reporttemplates.optional == 0  
%\>  
leads to  
“undefined method `optional’ for #Class:0x7f83e7451880”

Why does it tell me “undefined”, when one line above it tells me there  
is an attribute “optional”?

Any help is appreciated!

Best Regards,  
Daniel  
Süpke

---

<div class="post-metadata">

**Author:** ![Daniel\_SSSS\_pke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecae2f/32.png) [@Daniel\_SSSS\_pke](https://www.ruby-forum.com/u/Daniel_SSSS_pke)\
**Post date:** [December 21, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/undefined-method-error/155968/2 "2008-12-21T17:38:00Z")

</div>

c’mon guys, don’t leave me hanging on my very first rails question  
here ;).

It’s basically just: How can the debug output give tell me something  
is an object with attributes and then it doesn’t let me access this  
attributes? Maybe it’s a bug. Well, if no one has a clue I will try to  
find a bug tracker and post it there, but it’s probably just my lack  
of knowledge in ruby/rails.

Regards,  
Daniel  
Süpke

---

<div class="post-metadata">

**Author:** ![Daniel\_SSSS\_pke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecae2f/32.png) [@Daniel\_SSSS\_pke](https://www.ruby-forum.com/u/Daniel_SSSS_pke)\
**Post date:** [December 21, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/undefined-method-error/155968/3 "2008-12-21T18:54:21Z")

</div>

On 18 Dec 2008, at 16:22, Daniel Süpke wrote:

> But when I try to access the attribute “optional”, I always get an  
> error message!  
> \<% if articletemplate.articletemplates\_reporttemplates.optional == 0  
> %\>  
> leads to  
> “undefined method `optional’ for #Class:0x7f83e7451880”
> 
> Why does it tell me “undefined”, when one line above it tells me there  
> is an attribute “optional”?

Assuming you’re using the normal conventions,  
articletemplates\_reporttemplates is an array. it doesn’t have an  
optional attribute (but the elements inside it do)

Fred

---

<div class="post-metadata">

**Author:** ![Daniel\_SSSS\_pke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecae2f/32.png) [@Daniel\_SSSS\_pke](https://www.ruby-forum.com/u/Daniel_SSSS_pke)\
**Post date:** [December 22, 2008, 8:37am UTC](https://www.ruby-forum.com/t/undefined-method-error/155968/4 "2008-12-22T08:37:55Z")

</div>

Thanks a bunch, that helped. Didn’t know that “—” is meant to  
represent an array, YAML::dump could be more intuitive there. It only  
told me “object”, so I never even thought of an array. Well, now it  
works, so thanks again!
