Skip to content

Instantly share code, notes, and snippets.

@ZASMan
Last active February 1, 2018 16:54
Show Gist options
  • Save ZASMan/8b47b8f0b1ea8475389bc0bcdc33d08b to your computer and use it in GitHub Desktop.
Save ZASMan/8b47b8f0b1ea8475389bc0bcdc33d08b to your computer and use it in GitHub Desktop.
my_model_relationships.rb
class User < ApplicationRecord
has_many :notebooks, dependent: :destroy
has_one :user_name, foreign_key: "id", dependent: :destroy
# Has an attribute of first_name
# As well as an attribute of last_name
end
class Notebook < ApplicationRecord
belongs_to :user, autosave: true
has_many :email_logs, as: :email_loggable, dependent: :destroy
has_many :notebook_articles, dependent: :destroy
end
class NotebookArticle < ApplicationRecord
has_many :email_logs, as: :email_loggable, dependent: :destroy
belongs_to :notebook
has_one :user, through: :notebook
end
class EmailLog < ApplicationRecord
belongs_to :email_loggable, polymorphic: true
end
# Email Logs Controller
class EmailLogsController < ApplicationController
def index
@q = EmailLog.ransack(params[:q])
@q.sorts = ['email_log_type asc'] if @q.sorts.empty?
# Do I have to include email_loggable in results?
@email_logs = policy_scope(@q.result.includes(:email_loggable)).page(params[:page])
authorize @email_logs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment