Louis J Scoras
2005-Sep-29 13:50 UTC
[Rails] Active Record: Stand alone: code executes twice
Hi all; I''m trying to use active record to populate a database for querying account information on a bunch of Unix machines. Whenever the ":belongs_to users" association is declared in the following code, it seems to execute the script twice--that is it prints the 10 twice. If I remove the commented line, than it works correctly and only prints once. Other than that, the relationships seem to be working correctly. I''m wondering if this has something to do with how I''m accessing the library (no gems), from the location ubuntu installed the package to. Or, am I doing something else completely wrong? $:.unshift ''/usr/share/rails/activerecord/lib'' require ''active_record'' ActiveRecord::Base.establish_connection( :adapter => "mysql", :host => "localhost", :username => "scora002", :database => "accounts" ) class User < ActiveRecord::Base has_many :accounts end class Account < ActiveRecord::Base belongs_to :host belongs_to :user # This line seems to be belongs_to :primary_group, # a problem. :class_name => "Group", :foreign_key => "primary_group_id" has_and_belongs_to_many :groups end puts 10 Thanks! -- Lou
On 9/29/05, Louis J Scoras <louis.j.scoras-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all; > > I''m trying to use active record to populate a database for querying account > information on a bunch of Unix machines. > > Whenever the ":belongs_to users" association is declared in the following > code, it seems to execute the script twice--that is it prints the 10 twice. > If I remove the commented line, than it works correctly and only prints > once. Other than that, the relationships seem to be working correctly. > > I''m wondering if this has something to do with how I''m accessing the > library (no gems), from the location ubuntu installed the package to. Or, > am I doing something else completely wrong? > > $:.unshift ''/usr/share/rails/activerecord/lib'' > require ''active_record'' > > ActiveRecord::Base.establish_connection( > :adapter => "mysql", > :host => "localhost", > :username => "scora002", > :database => "accounts" > ) > > class User < ActiveRecord::Base > has_many :accounts > end > > class Account < ActiveRecord::Base > belongs_to :host > belongs_to :user # This > line seems to be > belongs_to :primary_group, # a > problem. > :class_name => "Group", > :foreign_key => "primary_group_id" > has_and_belongs_to_many :groups > end > > puts 10 > > Thanks! > > -- > LouI''ve noticed this behavior too. I profiled a couple of my models and noticed that it would do something like this: 1. Load Account 2. Load User from belongs_to :user 3. Load Account again from has_many :accounts Overriding methods doesn''t hurt, I just made sure in my own code that any method/attribute aliases were only called once. -- rick http://techno-weenie.net
On 9/29/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''ve noticed this behavior too. I profiled a couple of my models and > noticed that it would do something like this: > > 1. Load Account > 2. Load User from belongs_to :user > 3. Load Account again from has_many :accounts > > Overriding methods doesn''t hurt, I just made sure in my own code that > any method/attribute aliases were only called once. > > -- > rick > http://techno-weenie.net >Okay, cool. So I put the classes into seperate files and ''required'' them seperately. This seemed to make the problem go away. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails