Bionicman303
Bionicman303

Reputation: 1373

Join between two tables fails in Activerecord with uninitialized constant error

Using Activerecord (on a postgresql database) this program should do a simple inner join between the tables "cars" and "car_parts". Both tables exist in the db, are filled with data, and, keys are named by activerecord convention (i.e., primary keys in both tables are named "id"; foreign key in "car_parts" linking to "cars" is named "car_id"). I tried two alternatives by using "include" and "join" in Activerecord - unfortunately both failed with the below message. Any help appreciated, thanks! My program:

require 'active_record'

ActiveRecord::Base.establish_connection(
    # details left out
)

class Car < ActiveRecord::Base
  has_many :car_parts
end

class Car_Part < ActiveRecord::Base
  belongs_to :car
end

results2 = Car.joins(:car_parts)
results2.each{|row|  puts row.id}

results3 = Car.find :all, :include => [:car_parts]
results3.each{|row|  puts row.id}

For both results2 and results3 I receive the error message: "/home/myname/.rvm/gems/ruby-1.9.3-p194@global/gems/activerecord-3.2.8/lib/active_record/inheritance.rb:111:in `compute_type': uninitialized constant Car::CarPart (NameError)"

Upvotes: 1

Views: 995

Answers (1)

froderik
froderik

Reputation: 4808

You are mixing naming conventions. :car_parts refers to a class called CarPart whereas you are calling it Car_Part. Try to rename it to CarPart. (And embrace the next problem! :-) )

Active record thinks that a camel case part of a word should translate to one underscore.

Upvotes: 3

Related Questions