0

I have a students array like this

[#<Student id: 2, admission_no: "2", gender: "m", blood_group: "A">,#<Student id: 3, admission_no: "3", gender: "m", blood_group: "A">]

I am getting this array via named_scope .... So is there any way to select only required attributes with named scope... I need to delete admission_no and blood_group from this and return an array only with students id and gender.. How is it possible. Iam using rails2.3

3
  • 3
    Use Student.select('id, gender').find(:all) Commented Sep 11, 2013 at 7:22
  • Please paste your named_scope here. Commented Sep 11, 2013 at 7:37
  • Why do you need to not have them? Commented Sep 11, 2013 at 10:16

3 Answers 3

1

named_scope_result.select('id, gender') will give you your desired result.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to have an array of hashes containing only the required fields, starting from your array.

Student.select('id, gender').find(:all)

will do if you want to consider all the Student objects in your database.

Starting from a generic Student array: students, you can achieve what you want by:

result = Array.new
students.each |s| do
  data = { "id" => s.id, "gender" => s.gender }
  result << data
end

1 Comment

Would just Student.select('id, gender').find(:all).map(&:attributes) do the job?
0

do this Student.select('id, gender').find(:all)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.