"undefined method"-error

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

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

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

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!