Skip to content
lukasalexandre edited this page Dec 21, 2012 · 1 revision

###Belongs To A Belongs To association can be declared through the following method:

class Company
  include CsvRecord::Document

  attr_accessor :name
end

class Car
  include CsvRecord::Document

  belongs_to :company
end

company = Company.create :name => 'Chuts'

car = Car.new :model => 'F450'

car.company = company
# or
car.company_id = company.id

car.save

car.company # #<Company:0x007f9b249b24d8>

###Has Many Extending the previous example, you can use the has_many method to establish the inverse relationship:

class Company
  include CsvRecord::Document

  attr_accessor :name

  has_many :cars
end

company = Company.create :name => 'Chutz'

car.company = company
car.save

company.cars # [#<Car:0x007f9b249b24d8>]

###Has One The same as has_many but limited to one associated record.

class Company
  include CsvRecord::Document

  attr_accessor :name

  has_one :car
end

company = Company.create :name => 'Chutz'

car.save
company.car = car

company.car # #<Car:0x007f9b249b24d8>
Clone this wiki locally