Rails2 - ActiveRecord查询,根据相关记录的条件查找记录

时间:2016-06-09 11:00:47

标签: mysql activerecord ruby-on-rails-2

如何找到未评论特定用户(带有user_id)的所有帖子?

数据模型如下,

StringBuffer sbuffer = new StringBuffer();        
HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporterHTML.setExporterInput(exporterInput);

SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(sbuffer);
//pointing to the image servlet
HtmlResourceHandler imageHandler = new WebHtmlResourceHandler( request.getContextPath() + "/servlets/image?" + ImageServlet.IMAGE_NAME_REQUEST_PARAMETER
        + "={0}" + "&uuid=" + UUID.randomUUID().toString());
exporterOutput.setImageHandler(imageHandler);

//setting some sonfigs for the report
SimpleHtmlExporterConfiguration htmlConfig = new SimpleHtmlExporterConfiguration();
SimpleHtmlReportConfiguration htmlReportConfiguration = new SimpleHtmlReportConfiguration();
htmlConfig.setHtmlFooter("");
htmlConfig.setHtmlHeader("");
htmlConfig.setBetweenPagesHtml("");
htmlReportConfiguration.setPageIndex(pageIndex);

exporterHTML.setConfiguration(htmlConfig);
exporterHTML.setExporterOutput(exporterOutput);
exporterHTML.setConfiguration(htmlReportConfiguration);

exporterHTML.exportReport();

我尝试过如下操作,但它仍然会提供已由用户评论过的帖子,

User
----
(no direct association to Post and Comment)

Post
----
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
has_many :comments


Comment
-------
belongs_to :post
belongs_to :commentor, :class_name => "User", :foreign_key => "user_id"

我能够找到特定用户评论的所有帖子,如下所示,

Post.find(:all, :include => [:comments], :conditions => ["posts.user_id != ? AND comments.user_id != ?",  user.id, user.id])

请帮助我找到预期的结果。

0 个答案:

没有答案