Skip to content

Instantly share code, notes, and snippets.

@arenoir
Last active December 16, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arenoir/5409904 to your computer and use it in GitHub Desktop.
Save arenoir/5409904 to your computer and use it in GitHub Desktop.
ember-data polymorphic and rails model
App.List = DS.Model.extend
project: DS.BelongsTo(App.Project)
user: DS.BelongsTo(App.User)
task: DS.BelongsTo(App.Task)
proposal: DS.BelongsTo(App.Proposal)
class List < ActiveRecord::Base
attr_accessible :name, :project_id, :user_id, :task_id, proposal_id
belongs_to :listable, :polymorphic => true
%w(user project user proposal).each do |name|
class_eval %(
def #{name}_id
listable_id if listable_type == '#{name.classify}'
end
def #{name}_id=(_value)
self.listable_id = _value
self.listable_type = '#{name.classify}'
end
)
end
end
class ListSerializer < ActiveModel::Serializer
attributes :id, :name, :project_id, :user_id, :task_id, proposal_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment