0

I have a student array object which returns multiple attributes. I need to extract only specific attributes from this array. Here is the code that I have tried

@project.each do |p|
          @students << Student.find_by_id(:id => p.receiver_id, :select => "first_name, last_name")
        end

But it is showing Unknown keys :id. I need only the first name and the last name to be inserted in @students array. I am using rails 2.3 and ruby 1.8.7. Please help.

1 Answer 1

1

You are getting that error because it should be:

@project.each do |p|
  @students << Student.find_by_id(p.receiver_id)
end

If you only want the first names and last names, then you might consider:

@project.each do |p|
  student = Student.find_by_id(p.receiver_id)
  @students << { :first_name => student.first_name, :last_name => student.last_name }
end

If you want an array, then:

@project.each do |p|
  student = Student.find_by_id(p.receiver_id)
  @students << [student.first_name, student.last_name ]
end

The first version would give you an array of hashes. The second version would give you an array of arrays.

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

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.