简体   繁体   English

找到数组元素

[英]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 并希望通过word_id找到一个元素,例如

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

where params[:id2] is words.id 其中params[:id2]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? 当然这是错误的,因为数组索引与words.id不一样,那么我怎么能正确呢?

OR 要么

can you advice me on how to work with the model if I want to exclude some records from it? 如果我想从中排除一些记录,你能告诉我如何使用该模型吗?

@current_word = @words.detect{|w| w.id == params[:id2]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM