Forum: Ruby on Rails "undefined method"-error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel Süpke (Guest)
on 2008-12-18 18:35
(Received via mailing list)
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
Daniel Süpke (Guest)
on 2008-12-21 18:38
(Received via mailing list)
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
Frederick C. (Guest)
on 2008-12-21 19:54
(Received via mailing list)
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
Daniel Süpke (Guest)
on 2008-12-22 09:37
(Received via mailing list)
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!
This topic is locked and can not be replied to.