【发布时间】:2013-01-01 09:54:46
【问题描述】:
我有一个名为 Container 的 Rails 模型,其中有一列名为 products。它是 Postgres 和 'postgres_ext' gem 支持的字符串数组。
GEMFILE 的相关部分是:
gem 'rails', '3.2.9'
gem 'pg'
gem 'postgres_ext'
gem 'activerecord-postgres-hstore', git: 'git://github.com/engageis/activerecord-postgres-hstore.git'
迁移的相关部分是:
t.string :products, array: true
我正在我的Container 模型中编写一个公共方法,它将产品添加到这个数组中。该方法如下所示:
attr_accessible :products
def add_to_products(product)
if products.blank? || products.size == 0 ## product array is either uninstantiated or blank
products = [product.id]
else
unless products.include? product.id
products << product.id
end
end
end
这些是 irb/console 中的结果:
pry(main)> c = Container.first
=> #<Container id: "2765cc19-98f8-4e42-a1be-538788424ec7", name:....
pry(main)> p = Product.first
=> #<Product id: "319a25ae-87fe-4769-a9de-1a8e0db9e84f", name: ....
pry(main)> c.add_to_products(product)
pry(main)> c.products
=> nil
pry(main)> c.products= [] << "319a25ae-87fe-4769-a9de-1a8e0db9e84f"
pry(main)> c.products
=> ["319a25ae-87fe-4769-a9de-1a8e0db9e84f"]
我正在摸不着头脑想弄清楚add_to_products 方法有什么问题。有人可以对这种奇怪的情况有所了解吗?为什么我通过这个方法传值没有设置?
【问题讨论】:
-
“postgre”是指“PostgreSQL”吗?
-
这不是很明显吗?不明白评论的价值。编辑过的标题,不过如此。
-
不,不明显。评论的价值是为了澄清。 “PostgreSQL”是 DBM 的名称,有时缩写为“Postgres”,因为人们不喜欢说出整个名称。 (团队承认它很笨拙。)“Postgre”可能是那个或其他的,因为 StackOverflow 会收到来自世界各地用户的问题,而且他们并不总是拼写好或知道正确的名称。
标签: ruby-on-rails ruby rails-postgresql postgres-ext