Skip to content

Instantly share code, notes, and snippets.

@stevehodges
Last active August 29, 2015 14:22
Show Gist options
  • Save stevehodges/e85a1e367d546d527984 to your computer and use it in GitHub Desktop.
Save stevehodges/e85a1e367d546d527984 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
has_many :comments
belongs_to :approved_version, class_name: name
has_one :draft, class_name: name, foreign_key: :approved_version_id
amoeba do
include_association :comments
exclude_association [:approved, :draft]
customize(lambda {|live_obj, draft_obj|
# This only sets approved_version_id on Post model, doesn't get passed down to its child comments
draft_obj.approved_version_id = live_obj.id
})
end
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
belongs_to :approved_version, class_name: name
has_one :draft, class_name: name, foreign_key: :approved_version_id
amoeba do
include_association :ratings
exclude_association [:approved, :draft]
customize(lambda {|live_obj, draft_obj|
# This only sets approved_version_id on Comment model, doesn't get passed down to its child ratings
draft_obj.approved_version_id = live_obj.id
})
end
end
class Rating < ActiveRecord::Base
belongs_to :comment
belongs_to :approved_version, class_name: name
has_one :draft, class_name: name, foreign_key: :approved_version_id
# Ah crap, the approved_version_id isn't getting set
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment