Hi, I''m trying to do a custom method, but I get an undefined method error. Here''s what''s happening: <% @members.each do |member| %> <td><%= member.user.name %></td> <td><%= member.user.groupwide_score(@league) %></td> <td></td> <% end %> Can I add the method groupwide_score() to member.user, or will Rails think that I''m trying to find a column in the users table? Is there a way to do this? Thanks! Dave -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you can use instance_eval, class_eval or define_method to do that dynamically. Or just add it to the class via open objects. If this isn''t helpful enough could you elaborate a little more. class attributes? one instance or whole class? just this class or any class? . . . -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Jan 2008, at 02:56, Dave Amos wrote:> > Hi, I''m trying to do a custom method, but I get an undefined method > error. Here''s what''s happening: > > <% @members.each do |member| %> > <td><%= member.user.name %></td> > <td><%= member.user.groupwide_score(@league) %></td> > <td></td> > <% end %> > > Can I add the method groupwide_score() to member.user, or will Rails > think that I''m trying to find a column in the users table? Is there a > way to do this?You can of course add methods to the User class and you can also extend associations (which isn''t quite the same thing). Tell us a little bit more about what you are doing and what is going wrong and I''m sure someone can help you. 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, as I said before, I get an "undefined method" error. The code I showed you was in my show.html.erb file, and the controller code is in the groups_controller. I put the code for .groupwide_score() in the Groups model. Here''s what I got (ignore the SQL, I''m awful at it and still haven''t debugged it): def groupwide_score(league) query = "select * from guesses g, games ga where ga.id = g.game_id and ga.league_id = ? and g.user_id = ?" user_guesses = Guess.find_by_sql([query, id, league]) results = user_guesses.map { |g| g.points }.sum end I''m not sure why rails can''t seem to find the method, the model the correct place for it, right? Or should I put it in the User model? Frederick Cheung wrote:> On 24 Jan 2008, at 02:56, Dave Amos wrote: > >> Can I add the method groupwide_score() to member.user, or will Rails >> think that I''m trying to find a column in the users table? Is there a >> way to do this? > > > You can of course add methods to the User class and you can also > extend associations (which isn''t quite the same thing). > Tell us a little bit more about what you are doing and what is going > wrong and I''m sure someone can help you. > > Fred-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Jan 2008, at 15:26, Dave Amos wrote:> > Well, as I said before, I get an "undefined method" error. The code I > showed you was in my show.html.erb file, and the controller code is in > the groups_controller. I put the code for .groupwide_score() in the > Groups model. Here''s what I got (ignore the SQL, I''m awful at it and > still haven''t debugged it): > > def groupwide_score(league) > query = "select * from > guesses g, > games ga > where ga.id = g.game_id > and ga.league_id = ? > and g.user_id = ?" > user_guesses = Guess.find_by_sql([query, id, league]) > results = user_guesses.map { |g| g.points }.sum > end > > I''m not sure why rails can''t seem to find the method, the model the > correct place for it, right? Or should I put it in the User model? > >If it''s in the group model then member.user.groupwide_score can''t possibly work, because user is an instance of User, not Group (assuming you are using sane conventions). This is all rather tautological, but if you want to call a method on an instance of User you need to define it on user (or its superclass, a module included in it etc...) Fred> > Frederick Cheung wrote: >> On 24 Jan 2008, at 02:56, Dave Amos wrote: >> >>> Can I add the method groupwide_score() to member.user, or will Rails >>> think that I''m trying to find a column in the users table? Is >>> there a >>> way to do this? >> >> >> You can of course add methods to the User class and you can also >> extend associations (which isn''t quite the same thing). >> Tell us a little bit more about what you are doing and what is going >> wrong and I''m sure someone can help you. >> >> Fred > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 24, 2008, at 9:44 AM, Frederick Cheung wrote:> > tautological >I''ll be the first to admit that I had to look it up... tautology, n. 1. needless repetition of an idea, esp. in words other than those of the immediate context, without imparting additional force or clearness, as in "widow woman." 2. an instance of such repetition http://dictionary.reference.com/browse/tautological Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 24 Jan 2008, at 15:26, Dave Amos wrote: > >> games ga >> > If it''s in the group model then member.user.groupwide_score can''t > possibly work, because user is an instance of User, not Group > (assuming you are using sane conventions). This is all rather > tautological, but if you want to call a method on an instance of User > you need to define it on user (or its superclass, a module included in > it etc...) > > FredOkay, thanks! I''m still kinda new to programming, so some of that probably obvious logic isn''t so obvious to me. But I moved it to the User model and it worked like a charm. Thanks so much! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Amos wrote:> Hi, I''m trying to do a custom method, but I get an undefined method > error. Here''s what''s happening: > > <% @members.each do |member| %> > <td><%= member.user.name %></td> > <td><%= member.user.groupwide_score(@league) %></td> > <td></td> > <% end %> > > Can I add the method groupwide_score() to member.user, or will Rails > think that I''m trying to find a column in the users table? Is there a > way to do this? > > Thanks! > > DaveSimple answer is YES. Better answer is do you want to? The rest of the thread seems to be debating the second part. To do the first: class Member belongs_to :user, :options_go_here do def groupwide_score(league) #Go do that Voodoo that you do so well! end end end This is one of two ways to do exactly what you want. For more information check_out http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html and look in the "Association extensions" in the introduction. You can also check out my slightly long post about them here: http://startrader.wordpress.com/2008/01/18/three-great-features-of-rails/ -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---