Rails Magic FAQ

A grab bag of Rails tidbits that confuse beginners

Database and ActiveRecord

Naming and plurals

This is super confusing. Model name should be capitalized and singular, model file name is lower case. Corresponding database table is lower case plural. So:

# file: car_part.rb
# database table: car_parts
class CarPart
    ...
end

Associations

You need to put in both sides of the relation between two models, so:

class CarPart
   belongs_to :car
   ...
end

class Car
   has_many :car_parts
end

Many to many relations

  • Do not use has_and_belongs_to_many
  • Whenever you think many to many, you will need an extra “bridging” table