当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - each_with_index not working for an array rails

问题描述:

When each_with_index is used along with an array of active records returned from a query and when i call the attributes of the model. it says undefined method, for the attribute

Controller

@user_contact_query=Usercontact.select("*")

@available_contacts_array=Array.new

@user_contact_query.each do |variable|

@user_query=User.find( :all,:conditions => ["id = ?",variable.relator_id])

@available_contacts_array.push(@user_query)

end

View

<% @available_contacts_array.each_with_index do |variable, index| %>

<%= variable.email %>

<% end %>

网友答案:

Well, step by step, since this is rather bad code:

@user_contact_query = Usercontact.select("*")

the select part here is a bit strange, I would have used .all. Otherwise no problem here.

@available_contacts_array = Array.new

since in the next line you assign an array anyway there is no need to 'initialize' this variable here, you could just omit that line. (Most Ruby programmers would use [] to initialize an empty array in most cases)

@available_contacts_array = @user_contact_query.to_a

Assuming that we get some records back from the query we now should have an array with user contact objects. Again a simple .all would have had a similar effect as far as I understand what's going on. But assuming that Usercontact has email this is fine and what you expect it to be.

@user_contact_query.each do |variable|
  @user_query = User.find(:all, :conditions => ["id = ?", variable.relator_id])
  @available_contacts_array.push(@user_query)  
end

Now if I get this right what you do is you go through all Usercontacts you found, find every User related to it (possibly more than one) and then attach the result at the end of the array of Usercontacts. As a result you have a mix of Users and Usercontacts in that array. (Which still would work in your view if both have email, since thanks to duck typing Ruby would not care. But most likely one of both is missing email). Also if more than one User is found then an array of users is pushed. And an array for sure has no email.

Ok, with your edit it becomes simple. When you use :all with find then what you get is not a record but an array of records (even if there us only one.). You can just do:

User.find(variable.relator_id)

which will then find a single record and push that in the array. Which I guess is what you expect it to do.

There would be better ways in Rails to do this but this would require the models to be defined properly what your use of relator_id makes look unlikely.

网友答案:

First off please post your full error message. Although Without seeing the error I can tell with certainty that the issue is caused by you calling a method on a nil object i.e you think a value exists where it actually does not.

Besides that you really really should take more care of your code. The code you have right now is just plain horrible. This is an equivalent of your code:

@available = []

UserContact.find_each do |contact|
  @available << User.find(contact.relator_id)
end

An alternative is to use #inject

@available = UserContact.all.inject([]) do |list, contact|
  list << User.find(contact.relator_id)
  list
end

And the best solution would be to actually use ActiveRecord assocations here.

P.S your code does not make much sense anyhow. Why would you put users contacts and users both into that list?

网友答案:

Why don't you do just?

controller's helper:

@array = Usercontact.select( "*" ).map do| variable |
   User.find( :all,:conditions => [ "id = ?",variable.relator_id ])
end

view's helper:

@array.each.with_index do| variable, index |
   # ...
end

General note: you'be used in the @available_contacts_array variable mixed types, probably this lead to the error, please don't do in such matter.

分享给朋友:
您可能感兴趣的文章:
随机阅读: