Reputation: 71
I have table Field with :
t.string "name"
t.integer "id_survey"
t.integer "id_participant"
and I have the following instances in db:
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"]
and I have a variable:
nameStep = "p1_c1_exams"
I want to find the instance with the name ".... exams" and the greater number.
I tried with
lastField = Field.find{|f| f['id_participant']==1 and f['id_survey']==1 and f['name'].include? nameStep}['name']
but it find the first instance in the db "p1_c1_exams1" and not "p1_c1_exams2"
I tried then with:
lastField = Field.last{|f| f['id_participant']==1 and f['id_survey']==1 and f['name'].include? nameStep}['name']
but it find the last instance in the db "p1_c1_examination2" and not "p1_c1_exams2"
how can I write the query?
Upvotes: 0
Views: 69
Reputation: 5661
What are you doing there? Why don't you use Rails ActiveRecord?
Field.order(:id_participant, :id_survey, :name).where("name LIKE 'p1_c1_exams%'").last
This or something similar (I must admit not to 100% understand what you want to do) should do the trick.
If you can't do it this way, maybe you should consider changing your db and having a name field with the name only and moving those numbers and p1_c1_ stuff in different fields.
Upvotes: 2