Even with the new rails book, I''m doing something wrong with has_one and belongs_to and in need of help. Here''s the code in question: class Event < ActiveRecord::Base set_table_name "event" set_primary_key "event_id" has_one :grp, :foreign_key => "grp_id", :class_name => "Group" def Event.display( data, column_name ) g = Grp.new # error message (1) g = Event.Grp.new # error message (2) end end The Group class contains the :belong_to call. The error message is when Event.display is used: #1 uninitialized constant Grp #2 undefined method `Grp'' for Event:Class What am I doing wrong? I have tried using the ''grp'' variable in the event controller and model and I get the same error messages. Suggestions? Thoughts? Thanks, -- ==================================================| Steven Knight skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org | | UNH Research Computing Center 603-862-3042 | ===================================================
On 7/1/05, Steven Knight <skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org> wrote:> > Even with the new rails book, I''m doing something wrong with has_one and > belongs_to and in need of help. > > Here''s the code in question: > > class Event < ActiveRecord::Base > set_table_name "event" > set_primary_key "event_id" > has_one :grp, :foreign_key => "grp_id", :class_name => "Group" > def Event.display( data, column_name ) > g = Grp.new # error message (1)Is there a model called "Group"? Why don''t you just do: has_one :group, :foreign_key => "grp_id"> g = Event.Grp.new # error message (2)This makes no sense whatsoever. What are you trying to do?> end > end > > The Group class contains the :belong_to call. The error message is > when Event.display is used: > > #1 uninitialized constant Grp > #2 undefined method `Grp'' for Event:Class > > What am I doing wrong? I have tried using the ''grp'' variable in the > event controller and model and I get the same error messages. > > Suggestions? Thoughts? > > Thanks, > > -- > ==================================================> | Steven Knight skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org | > | UNH Research Computing Center 603-862-3042 | > ==================================================> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> class Event < ActiveRecord::Base > set_table_name "event" > set_primary_key "event_id" > has_one :grp, :foreign_key => "grp_id", :class_name => "Group" > def Event.display( data, column_name ) > g = Grp.new # error message (1) >Assuming you have a model called Group already defined, try: g = Group.new> g = Event.Grp.new # error message (2)I''m not sure what you''re trying to achieve here. You can either access the group (if it''s already been associated) by: g = self.grp Or, if you''re trying to add a new Group object to make it associated with this Event object: self << Group.new There is no need to reference the Event class when talking about Group objects, unless you mean a Group object associated with a particular instance of an Event. So it makes sense to use: g = Group.find(1) # Get an instance of Group from the database having id 1 associated_event = Group.event Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/1/05, Steven Knight <skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org> wrote:> > Even with the new rails book, I''m doing something wrong with has_one and > belongs_to and in need of help. > > Here''s the code in question: > > class Event < ActiveRecord::Base > set_table_name "event" > set_primary_key "event_id" > has_one :grp, :foreign_key => "grp_id", :class_name => "Group" > def Event.display( data, column_name ) > g = Grp.new # error message (1) > g = Event.Grp.new # error message (2) > end > end > > The Group class contains the :belong_to call. The error message is > when Event.display is used:1) if your events table has grp_id then Event belongs_to Group, so you essentially have has_one and belongs_to backward. And I think at that point you''d want Group has_many Events, unless there is a 1:1 mapping of the two models. 2) you need to use the name of the model, not the relation, when creating an instance, so g = Group.new. Or if ev was an instance of Event you could do ev.group = Group.new which would create a new group and set that events group to it. 3) forget you ever wrote the line "g = Event.Grp.new"... that''s just nonsense :) 4) Keep reading that book! With a little practice you''ll get the hang of it quickly> > #1 uninitialized constant Grp > #2 undefined method `Grp'' for Event:Class > > What am I doing wrong? I have tried using the ''grp'' variable in the > event controller and model and I get the same error messages. > > Suggestions? Thoughts? > > Thanks, > > -- > ==================================================> | Steven Knight skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org | > | UNH Research Computing Center 603-862-3042 | > ==================================================> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Jason
On Fri, Jul 01, 2005 at 03:17:56PM -0500, Jason Foreman wrote:> On 7/1/05, Steven Knight <skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org> wrote: > > > > Even with the new rails book, I''m doing something wrong with has_one and > > belongs_to and in need of help. > > > > Here''s the code in question: > > > > class Event < ActiveRecord::Base > > set_table_name "event" > > set_primary_key "event_id" > > has_one :grp, :foreign_key => "grp_id", :class_name => "Group" > > def Event.display( data, column_name ) > > g = Grp.new # error message (1) > > g = Event.Grp.new # error message (2) > > end > > end > > > > The Group class contains the :belong_to call. The error message is > > when Event.display is used: > > 1) if your events table has grp_id then Event belongs_to Group, so > you essentially have has_one and belongs_to backward. And I think at > that point you''d want Group has_many Events, unless there is a 1:1 > mapping of the two models.Yeah that makes sense.> > 2) you need to use the name of the model, not the relation, when > creating an instance, so g = Group.new. Or if ev was an instance of > Event you could do ev.group = Group.new which would create a new group > and set that events group to it.I see. The way I was reading the API docs, it read like I could use the name of the relation and not the name of the model. Using the model name removes the error messages. What I don''t understand is that I can comment out the belongs_to and has_many calls and still created, edit and show an Event. The bottom line is, what is belongs_to and has_many ( and friends ) doing? Thanks everyone for the help. Bye, -- ==================================================| Steven Knight skk-Y5xk1i+vvAw3uPMLIKxrzw@public.gmane.org | | UNH Research Computing Center 603-862-3042 | ===================================================