嵌套模型的急切加载问题
我有一些模型 - NewsArticle、Comment、User(如:author)和 Profile。
class NewsArticle < ActiveRecord::Base
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at', :include => 'translations'
end
class Comment < ActiveRecord::Base
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
belongs_to :commentable, :polymorphic => true, :counter_cache => true
default_scope :include => [{:author => :profile}, :translations]
end
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
正如你所看到的 - 我有 default_scope for Comment 来急切地加载作者的个人资料,但不幸的是它不起作用:( 我也尝试更新 NewsArticleController
def show
@news_article = NewsArticle.find(params[:id], :include => {:comments => {:author => :profile}})
@comments = @news_article.comments(:order => "created_at DESC")
respond_to do |format|
format.html
format.xml { render :xml => @news_article }
end
end
但没有任何改变:(
在使用 Comments 渲染 NewsArticle 时,我看到数据库的疯狂加载可以帮我优化一下吗?
PS:视图如下
news_articles/show.html.haml
.comments
%h2
%a{:id => 'comments', :name => 'comments'}
- if @news_article.comments_count == 0
No comments
- else
#{pluralize(@news_article.comments_count, I18n.t(:"global.words.comment"))}
%ul
- @comments.each do |comment|
= render :partial => "comment", :object => comment, :locals => {:source => source}
news_articles/_comment.html.haml
%li.comment.white-box
.title
%acronym{ :title => "#{comment.created_at.strftime(formatted_datetime)}"}
= comment.created_at.strftime(formatted_datetime)
%p
= I18n.t(:"global.words.by")
%a{ :href => "#" }
= link_to_author_of comment
.text
:cbmarkdown
#{comment.body}
%br/
.controls
= link_to I18n.t(:"flags.controls.flag"), flag_comment_path(comment, :source => source), :class => 'flag-link', :rel => 'nofollow'
= link_to I18n.t(:"comments.controls.destroy"), comment_path(comment, :source => source), :confirm => I18n.t(:"global.messages.are_you_sure"), :method => :delete
PPS:伙计们,抱歉 - 我有忘记通知您,模型用户和配置文件位于另一个数据库中,可以通过
establish_connection "accounts_#{RAILS_ENV}"
Current 访问 - 很明显为什么包含/连接不起作用,但也许您知道如何使用帐户数据优化对数据库的请求?
I have some models - NewsArticle, Comment, User (as :author) and Profile.
class NewsArticle < ActiveRecord::Base
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at', :include => 'translations'
end
class Comment < ActiveRecord::Base
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
belongs_to :commentable, :polymorphic => true, :counter_cache => true
default_scope :include => [{:author => :profile}, :translations]
end
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
As you can see - i have default_scope for Comment to eager load authors with profiles, but unfortunately it does not working :( Also i've tried to update NewsArticleController with
def show
@news_article = NewsArticle.find(params[:id], :include => {:comments => {:author => :profile}})
@comments = @news_article.comments(:order => "created_at DESC")
respond_to do |format|
format.html
format.xml { render :xml => @news_article }
end
end
but nothing changed :(
On rendering NewsArticle with Comments i see crazy load to database. Could you please help me with optimization?
PS: view is below
news_articles/show.html.haml
.comments
%h2
%a{:id => 'comments', :name => 'comments'}
- if @news_article.comments_count == 0
No comments
- else
#{pluralize(@news_article.comments_count, I18n.t(:"global.words.comment"))}
%ul
- @comments.each do |comment|
= render :partial => "comment", :object => comment, :locals => {:source => source}
news_articles/_comment.html.haml
%li.comment.white-box
.title
%acronym{ :title => "#{comment.created_at.strftime(formatted_datetime)}"}
= comment.created_at.strftime(formatted_datetime)
%p
= I18n.t(:"global.words.by")
%a{ :href => "#" }
= link_to_author_of comment
.text
:cbmarkdown
#{comment.body}
%br/
.controls
= link_to I18n.t(:"flags.controls.flag"), flag_comment_path(comment, :source => source), :class => 'flag-link', :rel => 'nofollow'
= link_to I18n.t(:"comments.controls.destroy"), comment_path(comment, :source => source), :confirm => I18n.t(:"global.messages.are_you_sure"), :method => :delete
PPS: Guys, sorry - i have forget to inform you that models User and Profile is located in another DB, that is accessed with
establish_connection "accounts_#{RAILS_ENV}"
Currently - its clear why include/joins does not working, but maybe you have any idea how to optimize requests to DB with accounts data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 :joins 而不是 :include 在您的 NewsArticle 中。查找
此链接可能会有所帮助
Rails :include 与 :joins
try :joins instead of :include in your NewsArticle.find
this link might be helpful
Rails :include vs. :joins