我有一些模型 - NewsArticle,Comment,User(as: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,但遗憾的是它不工作:(此外,我已经试过
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
,但没有更新NewsArticleController改变:(
在有评论渲染NewsArticle我看到疯狂的负载数据库能否请你帮我优化
PS:?争夺w是低于
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}"
目前 - 它清楚为什么包含/连接不起作用,但也许你有任何想法如何优化对帐户数据的DB请求?
能否请您从您的视图发布的代码? – 2010-08-11 17:04:16
更新了视图:) – 2010-08-11 20:43:55
我已经更新了我的问题:)对不起,因为缺少详细信息:) – 2010-08-14 06:41:20