尝试使用to_json生成嵌套的json格式时,ruby on rails中的未定义方法错误?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试使用to_json生成嵌套的json格式时,ruby on rails中的未定义方法错误?相关的知识,希望对你有一定的参考价值。

这是我的宝石lib s4s models文件夹中的产品型号:

module S4s
   module Models
     class Product < ActiveRecord::Base
       self.table_name = 'product'
       has_many :images, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
       has_many :primary_images, -> { where(primary_image: true) }, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
       has_one :image, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
       has_many :product_images, foreign_key: :product_id, class_name: 'S4s::Models::ProductImage'
       end
   end
end

这是我的gems lib s4s models文件夹中的product_image.rb文件:

require 'paperclip'

module S4s
 module Models
  class ProductImage < ActiveRecord::Base
  self.table_name = 'product_images'.freeze

  include S4s::Models::Concerns::Upload

  TYPE_HIGH_RESOLUTION = 'highResolution'
  TYPE_ADDITIONAL      = 'additional'

  IMAGE_VERSIONS = %w|mini small medium xs sm fullxs fullsm large|

  attr_accessor :image_file_size

  alias_attribute :image_file_name, :original_file_name
  alias_attribute :image_content_type, :file_ext
  alias_attribute :image_updated_at, :updated_at

  belongs_to :product, foreign_key: 'product_id'.freeze, class_name: 'S4s::Models::Product'.freeze
  belongs_to :color, foreign_key: 'color_id'.freeze, class_name: 'S4s::Models::Dictionary::Color'.freeze

  validates :title, presence: true

  scope :additional, -> { where(image_type: TYPE_ADDITIONAL) }
  scope :high_resolution, -> { where(image_type: TYPE_HIGH_RESOLUTION) }
  scope :primary_images, -> { where(primary_image: true) }

  after_initialize :set_default_value
  after_save :set_product_colors!

  add_attachment :image,
                 styles:     {
                     mini:   ['100x100#', :jpg],
                     small:  ['220x220#', :jpg],
                     medium: ['380x380#', :jpg],
                     xs:     ['240x240', :jpg],
                     sm:     ['348x348#', :jpg],
                     fullxs: ['480x480#', :jpg],
                     fullsm: ['768x768#', :jpg],
                     large:  ['1000x1000', :jpg],

                 },
                 path_block: -> (style) { self.build_path(style) },
                 matches:    /(png|jpe?g|gif)/i


  # Populate file_name attribute with the current title
  before_image_post_process :set_file_name!

  public

  def url(type = 'mini')
    return nil unless self.product_id.present?

    image.url(type)
  end

  def urls
    Hash[IMAGE_VERSIONS.map { |v| [v, self.url(v)] }]
  end

  def as_json(opts = {})
    {
        id:               self.id,
        is_primary_image: primary_image?,
        product_id:       self.product_id,
        title:            self.title,
        color:            'n/a',
        sku:              self.sku,
        position:         self.position,
        image_type:       self.image_type,
        urls:             self.urls
    }
  end

  def build_path(style)
    return nil if product.nil?

    build_asset_path(style, !(new_system? || title_used?))
  end

  private

  def build_asset_path(style, old_format = false)
    "/products/#{product_id}/#{build_slug(old_format)}-#{style}.#{_find_extension(image_content_type)}"
  end

  def build_slug(old_format)
    if old_format && !file_name.present?
      "#{product.name.parameterize}#{position > 0 ? "-#{position}" : ''}"
    else
      file_name
    end
  end

  def set_product_colors!
    _colors = self.product.images.map(&:color).compact

    if self.product.colors.map(&:id).sort != _colors.map(&:id).sort
      self.product.update_attribute :colors, _colors
    end
  end

  def set_file_name!
    self.file_name  = SecureRandom.hex(20)
    self.new_system = true
  end

  def set_default_value
    self.position   ||= 0
    self.new_system = true if self.new_system.nil?
  end
  end
 end
end

逻辑是我们使用S4s :: Models :: ModelName将这些模型调用到不同的应用程序

下面是我用来渲染json的控制器文件(这个控制器在另一个应用程序中):

class HalfsController < ApplicationController
    def index
      @hotspot = S4s::Models::Product.all
      render json: @hotspot.to_json(include: :product_images)
    end
    ...
end 

我需要在product对象中使用Product_image对象的嵌套格式。我是ruby和rails框架的新手,请帮帮我。

注意:我已经尝试了所有格式的to_json,例如:product_images和:product_image ..嵌套适用于宝石中的许多其他模型,但这些不适用于product和product_images ..他们使用paperclip上传和生成图片网址

答案

试试render json: @hotspot.as_json(include: :product_images)

我知道to_json在内部调用as_json,但如果你覆盖to_json,它将不包含关联。 (我认为)。

以上是关于尝试使用to_json生成嵌套的json格式时,ruby on rails中的未定义方法错误?的主要内容,如果未能解决你的问题,请参考以下文章

Pandas DataFrame to_json() 生成带点符号的索引

将 Pandas 数据框转换为嵌套 JSON

Ruby to_json 出现错误“非法/格式错误的 utf-8”问题

RoR 嵌套 :include 以在 to_xml/to_json 中包含子资源

熊猫在 to_json 时删除空值

何时在 Ruby 的 JSON 库中使用转储与生成与 to_json 以及加载与解析?