I''m slightly new at Rails, so any help would be appreciated! I have two models that have no association with each other (but they would probably be many-to-many). One is called ''Course'' and contains seed data of Golf Courses. The other is called ''Player'' and contains user entered information. Ultimately, I want to show a table in my view that displays all the golf courses appropriate for the player, based upon some calculations I''m doing in my Player model. However, I need to utilize some of the attributes from the Course model in my calculations. Right now, all my methods are in the Player model, so I''m doing something like this but am getting an ''undefined method ''Course'' error. What am I missing? Player.rb class Model < ActiveRecord::Base attr_accessible :attr_1, :attr_2, ...etc def handicap_differences Course.handicap (This is what I''m using to call the Course handicap, but it''s not working out) - player_handicap + some more math end end Thanks for the help! -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/NH-9w0U6OogJ. For more options, visit https://groups.google.com/groups/opt_out.
Jeffrey L. Taylor
2012-Sep-05 21:04 UTC
Re: Using one model''s attribute in another model''s method
Quoting sacshu <sacshu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> I''m slightly new at Rails, so any help would be appreciated! > > I have two models that have no association with each other (but they would > probably be many-to-many). One is called ''Course'' and contains seed data of > Golf Courses. The other is called ''Player'' and contains user entered > information. Ultimately, I want to show a table in my view that displays > all the golf courses appropriate for the player, based upon some > calculations I''m doing in my Player model. However, I need to utilize some > of the attributes from the Course model in my calculations. > > Right now, all my methods are in the Player model, so I''m doing something > like this but am getting an ''undefined method ''Course'' error. What am I > missing? > > Player.rb > class Model < ActiveRecord::Base > attr_accessible :attr_1, :attr_2, ...etc > > def handicap_differences > Course.handicap (This is what I''m using to call the Course handicap, > but it''s not working out) - player_handicap + some more math > end > end >Course.handicap() is a class method. I.e., not specific to any course instance. And you will have fewer problems if the class name matches the file name. E.g. in player.rb (all lower case). class Player < ActiveRecord::Base ... What is the class name in course.rb? Is it also Model? It should be Course. Jeffrey -- 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 https://groups.google.com/groups/opt_out.