Ruby on rails 从id'的数组中选择;s via包括
不知道如何解决我在选择个人时遇到的问题 数组中的id 我有一个商店、产品和价格的模型。商店有很多 (有很多)产品,每个商店有一个(有一个)价格。 我正在为数据库植入CSV数据。我的应用程序主要作为API使用 Prices.csv(其中ID为7和8的产品价格相同 店内价格为4.25美元(3): 创建_prices.rb:Ruby on rails 从id'的数组中选择;s via包括,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,不知道如何解决我在选择个人时遇到的问题 数组中的id 我有一个商店、产品和价格的模型。商店有很多 (有很多)产品,每个商店有一个(有一个)价格。 我正在为数据库植入CSV数据。我的应用程序主要作为API使用 Prices.csv(其中ID为7和8的产品价格相同 店内价格为4.25美元(3): 创建_prices.rb: create_table :prices do |t| ... t.integer :product_id, array: true, default: [] ...
create_table :prices do |t|
...
t.integer :product_id, array: true, default: []
... etc
end
当我试图包含价格时,问题出现在我的查询中
关联时,我收到一个错误声明:
ActiveRecord::StatementInvalid
(PG::UndefinedFunction: ERROR: operator does not exist: integer[] =
integer LINE 1: ...LEFT OUTER JOIN "prices" ON "prices"."product_id" =
"product...
我相信这个问题与价格包含的格式不适合处理数组有关
产品控制员:
class API::ProductsController < ApplicationController
@products = Product.where("products.store_id @> '{?}'", params[:id].to_i).includes(:price).where('prices.store_id = ?', params[:id]).references(:prices).limit(params[:max])
end
class API::ProductsController'{?}',params[:id]。to_i)。包括(:price)。其中('prices.store_id=?',params[:id])。引用(:prices)。限制(params[:max])
结束
Product.rb型号:
class Product < ActiveRecord::Base
belongs_to :store
has_one :price
def as_json(options)
super(:only => [:id, :name, :description, :image, :weight, :store_id],
:include => {
:price => {:only => [:amount, :store_id]}
}
)
end
end
类产品[:id,:name,:description,:image,:weight,:store\u id],
:include=>{
:price=>{:only=>[:amount,:store\u id]}
}
)
结束
结束
不要对prices product\u id使用数组数据类型,只需使用整数,并为每个商店/产品组合创建单独的记录
然后尝试使用has_one-through获取价格
class Product < ActiveRecord::Base
belongs_to :store
has_one :price, through: :store
仅对从其他源复制的内容使用引号格式。如果您引用的是某人,则需要提供适当的属性信息。@theTinMan-Oops,我现在已经删除了引号格式。我认为关联不支持使用这样的数组列
class Product < ActiveRecord::Base
belongs_to :store
has_one :price, through: :store
@products = Product.where(store_id: params[:id]).preload(:price)