将表的字段添加到序列化程序

时间:2018-06-01 12:39:48

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

我有三个表用户,帖子和评论。 post的序列化器是

class PostSerializer < ActiveModel::Serializer
    attributes :id, :content, :like, :created_at, :updated_at
attributes :user
has_many :comments
def user
    object.user
end
end

评论具有以下属性。评论序列化器

class CommentSerializer < ActiveModel::Serializer
    attributes :id, :comment, :created_at, :updated_at
attributes :user
attributes :post
belongs_to :user
belongs_to :post
def user
  object.user
end
def post
  object.post
end
end

当我打印帖子的JSON时,我需要注释的属性。

1 个答案:

答案 0 :(得分:0)

只需通过const webpackConfig = merge(baseWebpackConfig, { mode: 'production', devtool: config.build.productionSourceMap ? config.build.devtool : false, output: { path: config.build.assetsRoot, filename: utils.assetsPath('js/[name].[chunkhash].js'), chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') }, optimization: { splitChunks: { cacheGroups: { commons: { test: /[\\/]node_modules[\\/]/, name: "vendor", chunks: "all" }, }, }, concatenateModules: true, minimizer: [ new UglifyJsPlugin({ uglifyOptions: { compress: { dead_code: true, drop_console: true, }, output: { comments: false, }, ecma: 6, }, sourceMap: config.build.productionSourceMap, parallel: true }), new OptimizeCSSAssetsPlugin({}), ], }, plugins: [ // http://vuejs.github.io/vue-loader/en/workflow/production.html new CleanPlugin([dist], { root: path.resolve(__dirname, '..'), }), new VueLoaderPlugin(), new Dotenv({ path: path.resolve('.env') }), new MiniCssExtractPlugin({ filename: utils.assetsPath('css/[name].[hash].css'), chunkFilename: utils.assetsPath('css/[id].[hash].css') }), // generate dist index.html with correct asset hash for caching. // you can customize output by editing /index.html // see https://github.com/ampedandwired/html-webpack-plugin new HtmlWebpackPlugin({ filename: process.env.NODE_ENV === 'testing' ? 'index.html' : config.build.index, template: 'index.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode(a, b) { return (a.names[0] < b.names[0]) ? 1 : -1; }, }), // Generates manifest to configure pwa options new WebpackPwaManifest({ name: 'My Company', short_name: 'MyCompany', description: 'Vue + Vuetify progressive app with Loopback server', theme_color: "#004d40", start_url: "./", background_color: "#fafafa", icons: [ { "src": path.resolve('src/assets/logo.png'), "sizes": "200x200", "type": "image/png" }, { "src": path.resolve('src/assets/logo-512px.png'), "sizes": "512x512", "type": "image/png" }, ] }), // keep module.id stable when vendor modules does not change new webpack.HashedModuleIdsPlugin(), // copy custom static assets new WorkboxPlugin.GenerateSW({ swDest: path.join(config.build.assetsRoot, 'sw.js'), clientsClaim: true, skipWaiting: true, exclude: [/\.html$/], }), new HtmlCriticalPlugin({ base: path.resolve(__dirname, '../dist'), src: 'index.html', dest: 'index.html', inline: true, minify: true, extract: true, width: 375, height: 565, penthouse: { blockJSRequests: false, } }) ] }) // This option is enabled by default, if nginx or another server already implement // gzip you can disable it in config/index.js if (config.build.productionGzip) { const CompressionWebpackPlugin = require('compression-webpack-plugin') webpackConfig.plugins.push( new CompressionWebpackPlugin({ asset: '[path].gz[query]', algorithm: 'gzip', test: new RegExp( '\\.(' + config.build.productionGzipExtensions.join('|') + ')$' ), threshold: 10240, minRatio: 0.8 }) ) } if (config.build.productionBrotli) { const BrotliWebpackPlugin = require('brotli-webpack-plugin'); webpackConfig.plugins.push( new BrotliWebpackPlugin({ asset: '[path].br[query]', test: new RegExp( '\\.(' + config.build.productionBrotliExtensions.join('|') + ')$' ), threshold: 10240, minRatio: 0.8 }) ) } if (config.build.bundleAnalyzerReport) { const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin webpackConfig.plugins.push(new BundleAnalyzerPlugin()) } module.exports = webpackConfig

serializer关联中的has_many选项

更改

PostSerializer

has_many :comments

希望这有帮助