Rails 4:ActiveModelSerializer如何仅包含那些被批准的记录?

时间:2015-12-14 06:54:54

标签: ruby-on-rails ruby json active-model-serializers

在我的Rails 4.2 API中,我使用主动模型序列化程序来构建json响应。假设Post是一个模型,它有很多comments,我只想包含批准/发布的评论。我使用了名为approved的范围,该范围提供了批准的评论。

帖子的JSON响应包括所有评论,如何包含已批准的记录而不是所有内容。我如何构建Post序列化器。

class PostSerializer < ActiveModel::Serializer
  attributes :name, :body
  has_many :comments
end

3 个答案:

答案 0 :(得分:1)

class PostSerializer < ActiveModel::Serializer
  attributes :name, :body
  has_many :comments

  def comments
    object.comments.where( status: 'approved' )
  end
end

请参阅Active Model Serializers - overriding association methods

答案 1 :(得分:1)

序列化程序中的覆盖关联将起作用。在序列化程序中只需使用此方法覆盖

def comments   
  #Your comments filtering 
end

如果这不起作用,那么您的序列化程序版本就会出现问题。请查看此问题以获取更多详细信息和解决方法。 https://github.com/rails-api/active_model_serializers/issues/267

检查一下。 How do I select which attributes I want for active model serializers relationships

答案 2 :(得分:0)

bin/nutch solrindex http://127.0.0.1:8983/solr/files crawl/crawldb -linkdb crawl/linkdb crawl/segments/s1

使用INFO solr.SolrIndexWriter - Indexing 250 documents INFO solr.SolrIndexWriter - Deleting 0 documents INFO solr.SolrIndexWriter - Indexing 250 documents INFO solr.SolrIndexWriter - Deleting 0 documents INFO solr.SolrIndexWriter - Indexing 250 documents WARN mapred.LocalJobRunner - job_local1306504137_0001 java.lang.Exception: org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Unable to invoke function processAdd in script: update-script.js: Can't unambiguously select between fixed arity signatures [(java.lang.String, java.io.Reader), (java.lang.String, java.lang.String)] of the method org.apache.solr.analysis.TokenizerChain.tokenStream for argument types [java.lang.String, null] at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462) at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:529) Caused by: org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Unable to invoke function processAdd in script: update-script.js: Can't unambiguously select between fixed arity signatures [(java.lang.String, java.io.Reader), (java.lang.String, java.lang.String)] of the method org.apache.solr.analysis.TokenizerChain.tokenStream for argument types [java.lang.String, null] at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:552) at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210) at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206) at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:124) at org.apache.nutch.indexwriter.solr.SolrIndexWriter.write(SolrIndexWriter.java:134) at org.apache.nutch.indexer.IndexWriters.write(IndexWriters.java:85) at org.apache.nutch.indexer.IndexerOutputFormat$1.write(IndexerOutputFormat.java:50) at org.apache.nutch.indexer.IndexerOutputFormat$1.write(IndexerOutputFormat.java:41) at org.apache.hadoop.mapred.ReduceTask$OldTrackingRecordWriter.write(ReduceTask.java:493) at org.apache.hadoop.mapred.ReduceTask$3.collect(ReduceTask.java:422) at org.apache.nutch.indexer.IndexerMapReduce.reduce(IndexerMapReduce.java:356) at org.apache.nutch.indexer.IndexerMapReduce.reduce(IndexerMapReduce.java:56) at org.apache.hadoop.mapred.ReduceTask.runOldReducer(ReduceTask.java:444) at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:392) at org.apache.hadoop.mapred.LocalJobRunner$Job$ReduceTaskRunnable.run(LocalJobRunner.java:319) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) ERROR indexer.IndexingJob - Indexer: java.io.IOException: Job failed! at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:836) at org.apache.nutch.indexer.IndexingJob.index(IndexingJob.java:145) at org.apache.nutch.indexer.IndexingJob.run(IndexingJob.java:222) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70) at org.apache.nutch.indexer.IndexingJob.main(IndexingJob.java:231) 确定范围。仅获取状态为 class PostSerializer < ActiveModel::Serializer attributes :name, :body has_many :approved_comments, -> { where status: 'approved' }, class_name: 'Comment' end PostSerializer.includes(:approved_comments) 的评论。 从这里得到了这个概念 http://apidock.com/rails/ActiveRecord/Associations/ClassMethods