如何确定“多态控制器”的范围?到rails3多租户子域样式应用程序中的current_account
我正在尝试在帖子模型、上传模型等上使用多态注释模型。通常我会有一个 @parent 资源来将一个资源范围扩展到另一个资源,以便 Rails 建立关系。但因为这是一个多租户子域样式的应用程序,其中所有资源也需要限定在 current_account 范围内。我正在努力解决如何在 current_accout 下确定 @parent 资源的范围。
我有一个 current_account 方法、一个 find_parent 和一个 Parent_collection 方法
#Application_controller
class ApplicationController < ActionController::Base
before_filter :current_account
def current_account
unless is_root_domain?
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
@current_account
end
def find_parent
params.each do |name ,value|
@parent = $1.pluralize.classify.constantize.find(value) if name =~ /(.*?)_id/
return if @parent
end
end
def parent_collection
@parent_collection ||= current_account.send parent.pluralize
end
:
#comments_controller with only @parent resource without reference to current_account
class CommentsController < ApplicationController
before_filter :find_parent
def new
@comment = @parent.comments.build
end
def create
@comment = @parent.comments.build(params[:comment])
.....
.....
end
end
#comments_controller using only current_account resource without reference to @parent
class CommentsController < ApplicationController
before_filter :current_account
def new
@comment = current_account.comments.build
end
def create
@comment = current_account.comments.build(params[:comment])
.....
.....
end
end
在 ApplicationController 中, 应用程序_控制器。谢谢
I am trying to use a polymorphic comment model on the post model , upload model, etc. Ordinarily i would have a @parent resource to scope one to the other in order for Rails to build the relationship. But because this a multi-tenant subdomain styled application, where all resources will also need to be scoped to the curent_account. I am struggling with how to scope @parent resource under the current_accout.
In ApplicationController I have a current_account method, a find_parent and a parent_collection method:
#Application_controller
class ApplicationController < ActionController::Base
before_filter :current_account
def current_account
unless is_root_domain?
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
@current_account
end
def find_parent
params.each do |name ,value|
@parent = $1.pluralize.classify.constantize.find(value) if name =~ /(.*?)_id/
return if @parent
end
end
def parent_collection
@parent_collection ||= current_account.send parent.pluralize
end
end
#comments_controller with only @parent resource without reference to current_account
class CommentsController < ApplicationController
before_filter :find_parent
def new
@comment = @parent.comments.build
end
def create
@comment = @parent.comments.build(params[:comment])
.....
.....
end
end
#comments_controller using only current_account resource without reference to @parent
class CommentsController < ApplicationController
before_filter :current_account
def new
@comment = current_account.comments.build
end
def create
@comment = current_account.comments.build(params[:comment])
.....
.....
end
end
Any guide on how to call current_accout in the controllers in a way that @parent is scoped to it and is there a need for the parent_collection method that i put in the applications_controller. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们假设您的 @parent 资源是一个
Post
模型。然后我会想象:a) 您的
Account
模型has_many :posts
b) 您的
Post
模型belongs_to :account
c) 您的
Post
模型has_many :comments, :as => :可评论
c) 您的
Comment
模型belongs_to :commentable, :polymorphic => true
因此,考虑到这一点,您应该能够构建这样的范围:
您还可以将其重构为可注释模型:
并在控制器中使用它,如下所示:
Let's make an assumption that your @parent resource is a
Post
model. Then I would imagine that:a) your
Account
modelhas_many :posts
b) your
Post
modelbelongs_to :account
c) your
Post
modelhas_many :comments, :as => :commentable
c) your
Comment
modelbelongs_to :commentable, :polymorphic => true
So with that in mind you should be able to build scopes like this:
You can also refactor it to the commentable model:
and the use it in the controller like this: