I am trying to do the following
<%= effort.user_id.collect(&:full_name) %>
When I run my app I get the following error: undefined method
`collect’ for 1:Fixnum
From my understanding I believe that this maybe an association error.
I may be incorrect.
User.rb
class User < ActiveRecord::Base
include ActiveModel::Validations
has_many :roles_users
has_many :roles, :through => :roles_users
has_many :projects
has_many :password_histories
has_many :efforts
Efforts.rb
class Effort < ActiveRecord::Base
belongs_to :project_task
belongs_to :user
end
Where am i going wrong?
On Aug 24, 11:17am, “[email protected]”
[email protected] wrote:
I am trying to do the following
<%= effort.user_id.collect(&:full_name) %>
When I run my app I get the following error: undefined method
`collect’ for 1:Fixnum
What is that supposed to do ? user_id is an integer so calling collect
on it doesn’t make any sense.
Fred
On 24 Aug 2011, at 11:17, [email protected] wrote:
I am trying to do the following
<%= effort.user_id.collect(&:full_name) %>
When I run my app I get the following error: undefined method
`collect’ for 1:Fixnum
From my understanding I believe that this maybe an association error.
I may be incorrect.
#collect is a method you find on Enumerable objects; for example,
Arrays. It is a synonym for #map, and lets you apply a block to each
element of the collection:
[1, 2, 3, 4].collect{|x| x * 2} # => [2, 4, 6, 8]
You can’t send #collect to an integer (which is what you’re doing here).
It’s undefined, and doesn’t make any sense anyway.
What are you trying to do with this line of code? My guess is that
you’re trying to get the full_name attribute of the user that the effort
belongs to.
If so, remember that the association gives you a method to get the full
User object, not just the User’s ID:
effort.user
Once you have that, you can call the #full_name method directly:
effort.user.full_name
Is that what you’re looking for?
Chris