多くのアクティビティを持つ可能性のあるユーザーモデルがあります(統計目的で、ユーザーが実行したアクションの一部を追跡しています)
class User < ActiveRecord::Base
has_many :activities
has_many :histories,:through => :activities # ?
end
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :history, :polymorphic => true
end
class BrowsingHistory < ActiveRecord::Base
has_many :activities,:as => :histories
end
class LoginHistory < ActiveRecord::Base
has_many :activities,:as => :histories
end
...
Activityクラスには a history_type:string、 a 、 ahistory_id:integerがあり、orオブジェクトをuser_id:integer保持できるようにしたい。BrowsingHistoryLoginHistory
私はポリモーフィックな関連付けをほとんど扱っていませんが、これまでのところ、エラーが発生し続けています: ActiveRecord::HasManyThroughAssociationPolymorphicSourceError:Cannot have a has_many :through association 'User#histories' on the polymorphic object 'History#history'. このトピックに関する関連する質問をいくつか読みましたが、解決策がわかりません。
編集:私はそれを手に入れたと思います(コンソールテストは機能しているようです)、確認していただけますか?
class User < ActiveRecord::Base
has_many :activities
has_many :login_histories,:through => :activities,:source => :history,:source_type => "LoginHistory"
end
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :history, :polymorphic => true
end
class BrowsingHistory < ActiveRecord::Base
has_many :activities,:as => :history
has_many :users,:through => :activities
end
class LoginHistory < ActiveRecord::Base
has_many :activities,:as => :history
has_many :users,:through => :activities
end