多相の関係で、複数のオブジェクトを単一のクエリで取得する必要があります。Rails 5:多相関係を介して複数のオブジェクトを取得する
class Permission < ApplicationRecord
belongs_to :permitable, polymorphic: true
belongs_to :user, optional: true
end
メディア::メディアシュモデル:
module Media
class Medias < ApplicationRecord
belongs_to :companies, optional: true, inverse_of: :medias
has_many :permissions, class_name: 'Permission', as: :permitable
has_many :users, through: :permissions
end
end
class User < ApplicationRecord
has_many :companies, through: :accounts
has_many :permissions
has_many :medias, class_name: 'Media::Medias', through: :permissions,
source: :permitable, source_type: 'Media::Medias'
end
その後、私の許可モデルは次のようになります。
私は、これは持っているユーザーモデルでは
会社のモデル:
class Company < ApplicationRecord
has_many :accounts, dependent: :destroy
has_many :users, through: :accounts
has_many :medias, class_name: 'Media::Medias', inverse_of: :company
end
はどのようにして、各メディアのcurrent_user
と企業のために、すべてのマスコミのためActiveRecord::Relation object
を返すのですか?それは、Permission
モデルを通して関係を使用しなければならないことが重要です、私はAccounts
を通して企業を得るべきではありません。今のところ私はcurrent_user
のすべてのメディアを取得するためにCurrent.user.medias
を使用できるようにモデルを設定しました。
ありがとうございました!ここで
更新
は私が試したいくつかのバージョンから得たものである:
[4] pry(main)> user.medias.includes(:companies).map{|media| media.companies.id }
Media::Medias Load (1.4ms) SELECT "media_medias".* FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
NameError: uninitialized constant Media::Medias::Companies
from /home/ubuntu/.rvm/gems/ruby-2.3.3/gems/activerecord-5.1.0/lib/active_record/inheritance.rb:166:in `compute_type'
[5] pry(main)> user.medias.preload(:companies).select(['medias.id', 'medias.company.id'])
Media::Medias Load (1.3ms) SELECT medias.id, medias.company.id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
=> #<Media::Medias::ActiveRecord_AssociationRelation:0x33dd094>
[6] pry(main)> user.medias.select(['medias.id', 'medias.company_id'])
Media::Medias Load (1.1ms) SELECT medias.id, medias.company_id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
=> #<Media::Medias::ActiveRecord_AssociationRelation:0x3413234>
アップデート2
[4] pry(main)> puts user.medias.preload(:companies).select(['medias.id', 'medias.company.id'])
Media::Medias Load (1.4ms) SELECT medias.id, medias.company.id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "medias"
LINE 1: SELECT medias.id, medias.company.id FROM "media_medias" INNE...
^
: SELECT medias.id, medias.company.id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2
from /home/ubuntu/.rvm/gems/ruby-2.3.3/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:620:in `async_exec'
[5] pry(main)> puts user.medias.select(['medias.id', 'medias.company_id'])
Media::Medias Load (1.2ms) SELECT medias.id, medias.company_id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "medias"
LINE 1: SELECT medias.id, medias.company_id FROM "media_medias" INNE...
^
: SELECT medias.id, medias.company_id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2
from /home/ubuntu/.rvm/gems/ruby-2.3.3/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:620:in `async_exec'
アップデート3
012あなたのUserクラスの関連を追加[1] pry(main)> User.find(2).medias.preload(:companies).select(['medias.id', 'medias.company.id'])
User Load (21.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
Media::Medias Load (1.7ms) SELECT medias.id, medias.company.id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
=> #<Media::Medias::ActiveRecord_AssociationRelation:0x17688f0>
[2] pry(main)> User.find(2).medias.select(['medias.id', 'medias.company_id'])
User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
Media::Medias Load (1.8ms) SELECT medias.id, medias.company_id FROM "media_medias" INNER JOIN "permissions" ON "media_medias"."id" = "permissions"."permitable_id" WHERE "permissions"."user_id" = $1 AND "permissions"."permitable_type" = $2 [["user_id", 2], ["permitable_type", "Media::Medias"]]
=> #<Media::Medias::ActiveRecord_AssociationRelation:0x2155d90>
あなたは 'current_user.medias.includesのために何を得るのです(:企業)'? –
@ Md.FarhanMemon私はこれをコンソールで見る: '[2] pry(main)> user.medias.includes(:companies) Media ::メディアロード(1.6ms)SELECT" media_medias "。* FROM" media_medias "INNER "user_id" = $ 1 AND "permissions"。 "permitable_type" = $ 2 [["" user_id "、2]、[ "Allowable_type"、 "Media :: Medias"]] =># ' –
matiss
@ Md.FarhanMemonそれは私の望む結果を与えてくれるとは思わない+私は両方を取り戻す必要がある - メディアと企業。 – matiss