1

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}

Mustafa Yılmaz
  • 103
  • 2
  • 11
  • 1
    According to the error, you're calling `.id` on an array. Find out why that happens. – Sergio Tulentsev Nov 10 '16 at 07:07
  • 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 Nov 10 '16 at 07:08
  • The error is , in some case your `row_array` is empty and you are expecting an hash in it. so when you call `.id` you got the above error. to prevent the error you can add check like `unless row_array.blank?` – Pardeep Saini Nov 10 '16 at 07:13
  • 2
    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 Nov 10 '16 at 07:15
  • 1
    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 Nov 10 '16 at 07:17
  • 1
    http://guides.rubyonrails.org/debugging_rails_applications.html – Casper Nov 10 '16 at 07:18
  • Are you sure that this error is related to exactly that line `field_name => row_array[0].id`? – phts Nov 10 '16 at 08:11
  • Can you try `<% row_array = Array(rows) %>` instead of `<%row_array = rows.to_a%>`? Or if you still see the same error, then `<% row_array = Array(rows).flatten %>` – Jagdeep Singh Nov 10 '16 at 08:13
  • which line _exactly_ raises the error? – Sergio Tulentsev Nov 10 '16 at 09:07
  • This line .<%= aggregate_link data, { field_name => row.id, "closed" => 0 }, @SergioTulentsev – Mustafa Yılmaz Nov 10 '16 at 09:10
  • In this case, why you're examining `row_array[0]`? It doesn't trigger the error. – Sergio Tulentsev Nov 10 '16 at 09:12
  • It does the same thing with row.id . Both give the same error. The real interesting thing here is this code works flawless with mysql. It crashes with postgresql. @SergioTulentsev – Mustafa Yılmaz Nov 10 '16 at 09:13
  • Interesting. Well, good luck with your debugging. – Sergio Tulentsev Nov 10 '16 at 09:16
  • I think you need to post the controller action code together with the code that populates the `rows` variable – ReggieB Nov 10 '16 at 10:33

1 Answers1

0

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!

Mustafa Yılmaz
  • 103
  • 2
  • 11