Skip to content

Instantly share code, notes, and snippets.

@abuisman
Created December 15, 2011 22:49
Show Gist options
  • Save abuisman/1483326 to your computer and use it in GitHub Desktop.
Save abuisman/1483326 to your computer and use it in GitHub Desktop.
Problem with :name of child
module AssetChild
# by defining a module this should allow us to make nice code which adds:
#
# - has_one :assets
# - adds an after_create hook which automaticly builds the asset.
# - at some latter point we can also add after_update - to update the asset it's timestamp.
# - give access to asset attributes such as name => @usecase.name instead of @usecase.asset.name
#
# -> http://weblog.jamisbuck.org/2007/1/17/concerns-in-activerecord
def self.included(base)
base.has_one :asset, :as => :assetchild
base.has_one :project, :through => :asset
end
end
class Actor < ActiveRecord::Base
# Is asset child
include AssetChild
accepts_nested_attributes_for :asset
end
class Asset < ActiveRecord::Base
belongs_to :assetchild, :polymorphic => true
accepts_nested_attributes_for :assetchild
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment