Skip to content

Instantly share code, notes, and snippets.

@jwood
Created December 24, 2010 15:12
Show Gist options
  • Save jwood/754326 to your computer and use it in GitHub Desktop.
Save jwood/754326 to your computer and use it in GitHub Desktop.
An example using the Tenacity gem
class Car
include MongoMapper::Document
include Tenacity
t_has_many :wheels
t_has_one :dashboard
end
class Wheel < ActiveRecord::Base
include Tenacity
t_belongs_to :car
end
class Dashboard < CouchRest::ExtendedDocument
include Tenacity
use_database MY_COUCHDB_DATABASE
t_belongs_to :car
end
car = Car.create
# Set the related object
dashboard = Dashboard.create({})
car.dashboard = dashboard
car.save
# Fetch related object from the respective database
car.dashboard
# Fetch related object id
car.dashboard.car_id
# Set related objects
wheel_1 = Wheel.create
wheel_2 = Wheel.create
wheel_3 = Wheel.create
wheels = [wheel_1, wheel_2, wheel_3]
car.wheels = wheels
car.save
wheel_1.car_id # car.id
# Fetch array of related objects from the respective database
car.wheels # [wheel_1, wheel_2, wheel_3]
# Fetch ids of related objects from the database
car.wheel_ids # [wheel_1.id, wheel_2.id, wheel_3.id]
# Add a related object to the collection
new_wheel = Wheel.create
car.wheels << new_wheel
car.save
car.wheels # [wheel_1, wheel_2, wheel_3, new_wheel]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment