dpatterson2008-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org
2011-Aug-24 10:17 UTC
undefined method `collect'' for 1:Fixnum
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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 24, 11:17 am, "dpatterson2...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org" <dpatterson2...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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> 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?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 Aug 2011, at 11:17, dpatterson2008-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.