I have a student array object which returns multiple attributes. I need
to extract only specific attributes from this array. How do I do it…
@project.each do |p|
@students << Student.find_by_id(:id => p.receiver_id,
:select => “first_name, last_name”)
end
I tried the above code. But it is showing Unknown keys :id
I need only first name and last name to be inserted in @students array.
I am using rails2.3 and ruby 1.8.7 . Please help.
How about smth like that:
@project.map do |p|
student = p.reciever
{
first_name: student.first_name,
last_name: student.last_name
}
end
PS. It asumes that Project model has_many :students
2013/8/26 ruby rails [email protected]
On 08/26/2013 09:45 AM, ruby rails wrote:
I tried the above code. But it is showing Unknown keys :id
I need only first name and last name to be inserted in @students array.
I am using rails2.3 and ruby 1.8.7 . Please help.
I think you maybe be able to do
Student.find_by_id(p.receiver_id, :select => “first_name, last_name”)
but you can definitely do
Student.find(p.receiver_id, :select => “first_name, last_name”)
-Justin
On Aug 26, 2013, at 11:45 AM, ruby rails [email protected] wrote:
I have a student array object which returns multiple attributes. I need
to extract only specific attributes from this array. How do I do it…
@project.each do |p|
@students << Student.find_by_id(:id => p.receiver_id,
The problem is that you’re mixing things together. Since you’re using
find_by_id (or even if you used plain old find) you just specify the
value of id you’re looking for. Putting it in as :id => p.receiver_id
confuses Rails, since it’s then part of the hash which AR is not
expecting.
Student.find_by_id(p.receiver, ) will work (note the difference?)