Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find array element

I have an array

@words = Word.find_all_by_lesson_id(params[:id]) - @user.words

and want to find one element by word_id, something like

@current_word = @words[params[:id2].to_i]

where params[:id2] is words.id

Of course it's wrong, because the arrays index is not the same as words.id, so how can I do it correct?

OR

can you advice me on how to work with the model if I want to exclude some records from it?

like image 910
alex Avatar asked Feb 25 '12 10:02

alex


People also ask

What is find () in JavaScript?

JavaScript Array find() The find() method returns the value of the first element that passes a test. The find() method executes a function for each array element. The find() method returns undefined if no elements are found.

How do you find the index of an element in an array?

indexOf() The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.


1 Answers

@current_word = @words.detect{|w| w.id == params[:id2]}
like image 148
megas Avatar answered Oct 27 '22 01:10

megas