I have 3 models named User, Photo and Report
Model Report
is a polymorphic class and is used to report users and photos and model User
has a polymorphic association with the Modal Photo
.
So the functionality is a user can report photos or any other users.
below is the migration file of Report
.
class CreateReports < ActiveRecord::Migration
def change
create_table :reports do |t|
t.integer :user_id
t.integer :reported_id
t.string :reported_type, default: "User"
t.string :note, default: ""
end
end
end
and below are the associations
class User < ActiveRecord::Base
...
has_many :photos, as: :attachment
has_many :reports, dependent: :destroy
...
end
class Photo < ActiveRecord::Base
...
belongs_to :attachment, :polymorphic: true
end
class Report < ActiveRecord::Base
belongs_to :user
belongs_to :reported, polymorphic: true
end
currently i use a query like below to get reported list of a user either by the user itself or by their photos.
Report.where("(reported_id=? AND reported_type=?) OR (reported_id in (?) AND reported_type=?)",
self.id, "User", self.photos.pluck(:id), "Photo")
So, i would like to know how can i achieve same thing with an has_many association ?
Please help me if there is any better way which i can achieve the same or correct me if i'm wrong.