Skip to content

Instantly share code, notes, and snippets.

@mzdravkov
Last active December 26, 2015 14:19
Show Gist options
  • Save mzdravkov/7164223 to your computer and use it in GitHub Desktop.
Save mzdravkov/7164223 to your computer and use it in GitHub Desktop.
Some associations code that doesn't want to work.
class GroupCalendar < Calendar # Calendar inherits ActiveRecord:Base
has_and_belongs_to_many :users
has_many :sub_inheritances
# has_many :sub_calendars, through: :sub_inheritances, as: :calendarable # <- this line should be as the following:
has_many :sub_calendars, through: :sub_inheritances, source: :calendarable, source_type: :Calendar
end
class SubInheritance < ActiveRecord::Base
belongs_to :group_calendar
belongs_to :calendarable, polymorphic: true
end
class CreateSubInheritances < ActiveRecord::Migration
def change
create_table :sub_inheritances do |t|
t.integer :group_calendar_id
t.references :calendarable, polymorphic: true
t.timestamps
end
end
end
# When I try to 'group_calendar.sub_calendars' in the rails console I get:
# "ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :sub_calendar or :sub_calendars in model SubInheritance. Try 'has_many :sub_calendars, :through => :sub_inheritances, :source => <name>'. Is it one of :group_calendar or :calendarable?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment