Hi everyone, Maybe you guys can help me with a question. I am getting an error with an instance variable in a view and a select list. <%=select("sub_season","id",@sub_seasons.collect {|s| [s.sub_season_type,s.id]}) %> undefined method `id'' for #<Array:0x0000010ea334f0> -- 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.
@sub_seasons = [ [1, 2], [10, 20], [100, 200] ] @sub_seasons.collect do |element| element.id end --output:-- undefined method `id'' for [1, 2]:Array (NoMethodError) -- Posted via http://www.ruby-forum.com/. -- 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 17 September 2011 20:49, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @sub_seasons = [ > [1, 2], > [10, 20], > [100, 200] > ] > > @sub_seasons.collect do |element| > element.id > end > > --output:-- > > undefined method `id'' for [1, 2]:Array (NoMethodError)I am not sure why you would expect anything else. @sub-seasons is an array. The collect method passes each element of @sub-seasons to the block, so the first time round element is an array [1,2]. An array does not have a method ''id'' hence the error. What were you expecting it to do? Colin -- gplus.to/clanlaw -- 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.
Colin Law wrote in post #1022477:> On 17 September 2011 20:49, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> --output:-- >> >> undefined method `id'' for [1, 2]:Array (NoMethodError) > > I am not sure why you would expect anything else. @sub-seasons is an > array. The collect method passes each element of @sub-seasons to the > block, so the first time round element is an array [1,2]. An array > does not have a method ''id'' hence the error. What were you expecting > it to do? >It''s interesting isn''t it, that what I posted has the same error that the op posted. Do you think that is just a coincidence? -- Posted via http://www.ruby-forum.com/. -- 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 18 September 2011 00:44, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> It''s interesting isn''t it, that what I posted has the same error that > the op posted. Do you think that is just a coincidence?Not really. It was contrived, not a coincidence. It may have been interesting had you done the same thing as the OP... but you missed out one part of the collect. It would be more helpful if you''d asked the OP to confirm what was in the @sub_seasons variable - to double-check there wasn''t an array of arrays in it. It sure is possible that she''d already performed a collect. But you didn''t - you belittle without any explanation, and as you more frequently are than not: you''re wrong. Now if I wished to be as snide and sarcastic as you, I''d leave my post there, but instead, I''ll suggest that *maybe* if you''d copied the OP''s collect instead of spitefully hinting at your perceived superiority, the error you''d get would actually have been: NoMethodError: undefined method `sub_season_type'' for [1, 2]:Array ...seeing as the original collect accesses .sub_season_type before it accesses .id }:-[ Now, OP, can you please show the code that populates @sub_seasons in the controller - as I think the problem is something to do with what that''s being filled with - it may be an array of arrays, or something else, but it would be nice to see. -- 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 18 September 2011 00:44, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1022477: >> On 17 September 2011 20:49, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> --output:-- >>> >>> undefined method `id'' for [1, 2]:Array (NoMethodError) >> >> I am not sure why you would expect anything else. @sub-seasons is an >> array. The collect method passes each element of @sub-seasons to the >> block, so the first time round element is an array [1,2]. An array >> does not have a method ''id'' hence the error. What were you expecting >> it to do? >> > > It''s interesting isn''t it, that what I posted has the same error that > the op posted. Do you think that is just a coincidence?@7Stud: Had you quoted anything in your post I might have realised that this was in response to a previous question. As it was, your post appeared to be a request for help in understanding some code. Please try to make your posts more clear in order to avoid wasting my time. Thanks. Colin -- gplus.to/clanlaw -- 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.
> > Now, OP, can you please show the code that populates @sub_seasons in > the controller - as I think the problem is something to do with what > that''s being filled with - it may be an array of arrays, or something > else, but it would be nice to see.Another thing to check is the value of @sub_season - the select helper will try to call id on that too. Fred -- 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 18 September 2011 08:32, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Another thing to check is the value of @sub_season - the select helper > will try to call id on that too.*bingo* I''d say :-) yes... I''m moving my chips to that square - the helper looks for a default value for the :model and :method you''ve given it (:sub_season and :id in the original quote) -- 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.