each_with_index is used along with an
active records returned from a query and when i call the
attributes of the model. it says
undefined method, for the
@user_contact_query.each do |variable|
@user_query=User.find( :all,:conditions => ["id = ?",variable.relator_id])
<% @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
@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
Ok, with your edit it becomes simple. When you use
find then what you get is not a record but an array of records (even if there us only one.). You can just do:
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?
@array = Usercontact.select( "*" ).map do| variable | User.find( :all,:conditions => [ "id = ?",variable.relator_id ]) end
@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.