Skip to content

Instantly share code, notes, and snippets.

Created December 1, 2013 15:57
Show Gist options
  • Save anonymous/7735622 to your computer and use it in GitHub Desktop.
Save anonymous/7735622 to your computer and use it in GitHub Desktop.
class Feature < ActiveRecord::Base
has_one :project_item, as: :item, class_name: "ProjectItem"
end
class Milestone < ActiveRecord::Base
has_one :project_item, as: :item, class_name: "ProjectItem"
end
Project.first.items # fails, undefined method 'items'
Project.first.project_items # no results ever
Project.first.features # could not find the association :item in model Project
Project.first.milestones # could not find the association :item in model Project
ProjectItem.first.feature # undefined method 'feature'
ProjectItem.first.milestone # undefined method 'milestone'
Feature.first.project_item.project.name # works, but expected Feature.first.item.project.name
class Project < ActiveRecord::Base
has_many :project_items, as: :item, class_name: "ProjectItem", dependent: :destroy
has_many :features, through: :item, class_name: "ProjectItem", dependent: :destroy
has_many :milestones, through: :item, class_name: "ProjectItem", dependent: :destroy
end
class ProjectItem < ActiveRecord::Base
belongs_to :item, touch: true, polymorphic: true
belongs_to :project, touch: true
end
create_table "project_items", force: true do |t|
t.integer "project_id"
t.integer "item_id"
t.string "item_type"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "features", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "milestones", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment