在Rails中使用多态模型连接数据库视图 [英] Connecting database view with polymorphic model in rails
问题描述
我具有以下设置,该设置允许用户拥有多个项目,并且每个项目可以具有多个任务.用户可以收藏多个项目.
I have the following setup which allows a user to have multiple projects and each project can have multiple tasks. A user can favourite multiple projects.
class User < ActiveRecord::Base
has_many :projects
has_many :tasks, :through => :projects
has_many :favourites
has_many :favourite_projects, :through => :favourites, :source => :favourable, :source_type => "Project"
has_many :favourite_tasks, :through => :favourite_projects, :source => :tasks
...
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :tasks
has_many :favourites, :as => :favourable
...
end
class Task < ActiveRecord::Base
belongs_to :project
...
end
class Favourite < ActiveRecord::Base
belongs_to :user
belongs_to :favourable, :polymorphic => true
...
end
此设置允许@user.favourite_tasks
列出他们喜欢的项目的所有任务.
This setup allows @user.favourite_tasks
to list all the tasks for the projects that they have favourited.
从这里接受建议( http://pivotallabs.com/database-views-performance -rails/)我正在尝试尽可能用数据库视图替换多级表联接.
Taking the suggestion from here (http://pivotallabs.com/database-views-performance-rails/) I am trying to replace the multi-level table joins with a database view where possible instead.
视图SQL是:
SELECT tasks.id AS task_id, ..., projects.id AS project_id, ...
FROM tasks INNER JOIN projects ON projects.id = tasks.project_id
我的新ProjectTask
模型是:
class ProjectTask < ActiveRecord::Base
self.primary_key = 'task_id'
end
我已经更新了我的User
模型,使其包括:
I've updated my User
model to include:
has_many :project_tasks
对@user.project_tasks
正常.
但是,我无法弄清楚模型中的has_many/has_one/belongs_to
应该是什么,以便收藏夹可以工作(将收藏夹连接到视图而不是项目表).
However, I can't figure out what the has_many/has_one/belongs_to
should be in the models for the favourites to work (connecting the favourites to the view rather than the projects table).
我的目标是has _many :favourite_project_tasks, :through => :favourites....
,以便可以在控制器中使用@user.favourite_project_tasks
并在需要时将任何ProjectTask
模型范围附加到该范围.
我认为ProjectTask
模型具有task_id
作为主键的事实正在导致链接表/视图的轨道问题,并且因为使用:through
会覆盖对:foreign_key
和:primary_key
的任何使用(根据 http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many ).
I'm aiming for has _many :favourite_project_tasks, :through => :favourites....
so that I can use @user.favourite_project_tasks
in my controller and attach any ProjectTask
model scopes to it if need be.
I think the fact that the ProjectTask
model has task_id
as the primary key is causing issue with rails linking the tables/view and because using :through
overrides any use of :foreign_key
and :primary_key
(according to http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many).
希望有人可以建议我应该做些什么,因为我已经尝试了很多变化的组合,但并不高兴.
Hopefully someone can advise what I should be doing because I've tried loads of combinations of changes with no joy.
谢谢
推荐答案
问题似乎是由自定义主键引起的.
Issue seems to have been caused by the custom primary key.
通过使用User
模型更新(失败时我已经在执行此操作):
By updating the User
model with (I was already doing this when it was failing):
has_many :favourite_project_tasks, :through => :favourite_projects, :source => :project_tasks
使用视图,也将视图更改为使用:
to use the view and also changing the view to use:
SELECT tasks.id AS id, ...
而不是:
SELECT tasks.id AS task_id, ...
,并更改ProjectTask
视图模型以使用:
and changing the ProjectTask
view model to use:
self.primary_key = :id
现在可以使用.
这篇关于在Rails中使用多态模型连接数据库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!