Ruby on rails 创建要发送回客户端的JSON对象
感谢所有迄今为止帮助过我的人。我只有最后一个问题,我想我可以取消这个学习项目 我想创建一个JSON对象,如下所示:Ruby on rails 创建要发送回客户端的JSON对象,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,感谢所有迄今为止帮助过我的人。我只有最后一个问题,我想我可以取消这个学习项目 我想创建一个JSON对象,如下所示: [ {name: 'xbox', upc: '1313413412', available_on: '12/31/18', properties: [ {name: 'material', value: 'plastic' }, {name: 'color',
[
{name: 'xbox',
upc: '1313413412',
available_on: '12/31/18',
properties: [
{name: 'material',
value: 'plastic'
},
{name: 'color',
value: 'white'
},
]
},
{name: 'nintendo',
upc: '1313413412',
available_on: '12/31/18',
properties: [
{name: 'material',
value: 'plastic'
},
{name: 'color',
value: 'black'
},
]
}
]
我有三个表产品,属性,产品属性
我通过这样做加入他们,这是我的控制器:
@products = Product.joins(:properties, :product_properties)
@products.each do |product|
end
但我不知道该从这里走到哪里。我为这个新问题道歉。我只是想在实践中学习。我的关联设置正确
模式:
ActiveRecord::Schema.define(version: 2018_09_24_163027) do
create_table "product_properties", force: :cascade do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "product_id"
t.integer "property_id"
t.index ["product_id"], name: "index_product_properties_on_product_id"
t.index ["property_id"], name: "index_product_properties_on_property_id"
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "upc"
t.datetime "available_on"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "properties", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "product_id"
t.index ["product_id"], name: "index_properties_on_product_id"
end
end
再次感谢你能给我的任何帮助
@products.map do |product|
{
name: product.name,
properties: product.product_properties.map do |product_property|
{
name: product_property.property.name,
value: product_property.value
}
end
}
end.to_json
您可以随意设置所需的属性。您提到您需要一个JSON,但实际上您粘贴了一个Ruby哈希,我在最后添加了一个。\u JSON
您可以随意设置所需的属性。您提到您需要一个JSON,但实际上您粘贴了一个Ruby哈希,我在最后添加了一个
。到了\u JSON
。首先,您需要定义模型并设置它们之间的关系。使用示例中的表,模型类将是:
class Product < ApplicationRecord
has_many :product_properties
has_many :properties, through: :product_properties
end
class Property < ApplicationRecord
has_many :product_properties
has_many :products, through: :product_properties
end
class ProductProperty < ApplicationRecord
belongs_to :product
belongs_to :property
end
尽管查询优化会浪费空间,但我认为这对于学习项目来说是不错的。首先,您需要定义模型并设置它们之间的关系。使用示例中的表,模型类将是:
class Product < ApplicationRecord
has_many :product_properties
has_many :properties, through: :product_properties
end
class Property < ApplicationRecord
has_many :product_properties
has_many :products, through: :product_properties
end
class ProductProperty < ApplicationRecord
belongs_to :product
belongs_to :property
end
虽然查询优化有一个浪费空间,但是,我认为这对于学习项目是很好的。虽然这可能不是优化查询的一个重要位置,但仍然应该避免留下N+1个查询。抱歉,我刚刚注意到
properties
表有一个字段product\u id
。原因是什么?表products
和properties
已经通过联接表product\u properties
(它也包含值
)进行了连接。@IlyaKonyukhov这是一个我需要纠正的错误。谢谢你的回答@MarcinKołodziej Yup,谢谢你的评论。更正了我的回答虽然这可能不是优化查询的重要位置,但仍应避免留下N+1个查询。抱歉,我刚刚注意到properties
表中有一个字段product\u id
。原因是什么?表products
和properties
已经通过联接表product\u properties
(它也包含值
)进行了连接。@IlyaKonyukhov这是一个我需要纠正的错误。谢谢你的回答@MarcinKołodziej Yup,谢谢你的评论。更正了我的回答,非常感谢!只有一个问题。我似乎无法从第二张地图中得到价值。我想通过我的协会,我可以做一些类似property.product\u properties.value的事情。那太好了,谢谢!只有一个问题。我似乎无法从第二张地图中得到价值。我想通过我的联想,我可以做一些类似property.product\u properties.value的事情。