0
我有表格字段:困難,與去年
t.string "name"
t.integer "id_survey"
t.integer "id_participant"
和我有分貝以下實例:
Field1= ["id_participant" => 1, "id_survey" => 1, "name" => "p1_c1_exams1"]
Field2= ["id_participant" => 1, "id_survey" => 1, "name" => "p1_c1_exams2"]
Field3= ["id_participant" => 1, "id_survey" => 1, "name" => "p1_c1_examination1"]
Field4= ["id_participant" => 1, "id_survey" => 1, "name" => "p1_c1_examination2"]
,我有一個變量:
nameStep = "p1_c1_exams"
我想找到名稱爲「....考試」和更多的實例。
我試着用
lastField = Field.find{|f| f['id_participant']==1 and f['id_survey']==1 and f['name'].include? nameStep}['name']
,但它發現在DB 「p1_c1_exams1」,而不是 「p1_c1_exams2」
我再嘗試的第一個實例:
lastField = Field.last{|f| f['id_participant']==1 and f['id_survey']==1 and f['name'].include? nameStep}['name']
但它找到數據庫中的最後一個實例「p1_c1_examination2」,而不是「p1_c1_exams2」
我怎麼能w請問這個問題?
感謝您的建議,我修改了一點您的查詢,我得到了預期的結果。 Field.order(:name).where(「id_participant =#{@ idOfParticipant} AND id_survey =#{@ idOfSurvey} AND name LIKE'#{nameStep}%'」)。last ['name'] –