1
votes

i'm learning Ruby on Rails and working on redmine and some redmine plugins currently.

<% for row in rows %>
<%row_array = rows.to_a%>
<tr class="<%= cycle("odd", "even") %>">
  <td class="name"><%= link_to h(row), aggregate_path(@project, field_name, row)  %></td>
  <td>
    <%
      aggregate_link data, { field_name => row_array[0].id, "closed" => 0 },
                     aggregate_path(@project, field_name, row,
                                    :op => {"status_id"=>"o", "#{filter_by}"=>"><"},
                                    "v[#{filter_by}]" => formated_dates(@dates),
                                    "f" => ["status_id", "#{filter_by}", ""])
    %>

For some reason this code throws

"ActionView::Template::Error (undefined method `id' for []:Array):"

rows is an ActiveRelation of Tracker class which is defined as

class Tracker < ActiveRecord::Base

What i understand from here is i should access the properties of row objects somehow, and i do could print them on to console but i still get the same error no matter what i do.

Thing i tried are more or less : row.attributes[:id] , row.attributes["id"], row.id . All of these prints the correct variable but throws an error when i want to access them here field_name => row_array[0].id

PS : Don't worry about the open tags, i didn't paste all of it, there are no errors there as far as i know.

Thanks in advance!

Edit : When i debug it the row object has a structure like this :

row(Tracker class) -> @attributes(ActiveRecord:AttributeSet) -> @attributes(LazyHashSet) -> @values(Hash) -> id.

Edit 2 : puts row_array[0].methods prints these :

id

id=

id_before_type_cast

id_came_from_user?

id?

id_changed?

id_change

id_will_change!

id_was

reset_id!

restore_id!

So i believe my method calling is correct.

Edit 3 : row_array[0].attributes outputs this :

{"id"=>1, "name"=>"Hata", "is_in_chlog"=>true, "position"=>1, "is_in_roadmap"=>false, "fields_bits"=>0, "default_status_id"=>1}

1
According to the error, you're calling .id on an array. Find out why that happens. - Sergio Tulentsev
I don't see obvious errors with the code. Either the problem is in other code somewhere, or in data, or is env problem (you changed code on disk, but your server still runs old buggy version, something like that) - Sergio Tulentsev
I believe this is not corect: <%row_array = rows.to_a%>... This is inside the <% for row in rows %> and I think you meant <%row_array = row.to_a%>, without the s in rows. - Ed de Almeida
It's simple to figure out these errors if you learn to use the debug statement. Just doing <%= debug row_array %> would probably already have given enough clues to fix it without guessing too much what could be wrong. - Casper

1 Answers

0
votes

Alright so the problem was a wrong SQL query which was returning wrong values, therefore some of them didn't had .id method in them. Corrected the SQl query and tried to access the id by ActiveRecord::Relation["id"] solved the problem.

Thank you for everyone that tried to help!