Search code examples
ruby-on-railsrubyredmineredmine-plugins

Ruby undefined method error


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}


Solution

  • 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!