Skip to content

Instantly share code, notes, and snippets.

@tubbo
Forked from laspluviosillas/polymorphic.rb
Last active April 13, 2017 20:22
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 tubbo/009e77ac61f872ff363978a9301cbba0 to your computer and use it in GitHub Desktop.
Save tubbo/009e77ac61f872ff363978a9301cbba0 to your computer and use it in GitHub Desktop.
Issue with polymorphic associations
class Task < ActiveRecord::Base
has_one :notification_preference, as: :notifee
accepts_nested_attributes_for :notification_preference
end
class NotificationPreference < ActiveRecord::Base
belongs_to :notifee, polymorphic: true
end
# accepts_nested_attributes_for allows you to do this:
Task.find_or_initialize_by(
notification_preference_attributes: {
# attributes for the NP
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment