- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想象一个模型结构如下:
class CrossSellPromotion < Promotion
has_and_belongs_to_many :afflicted_products, :join_table => :promotion_afflicted_products, :foreign_key => 'promotion_id', :association_foreign_key => 'product_id', :class_name => 'Product'
has_and_belongs_to_many :afflicted_categories, :join_table => :promotion_afflicted_categories, :foreign_key => 'promotion_id', :association_foreign_key => 'category_id', :class_name => 'Category'
. . .
end
class Promotion < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :promotions_products
has_and_belongs_to_many :categories, :join_table => :promotions_categories
- - -
end
和表格如下:
table promotions
type :string
...some other fields unrelated to this problem
table promotions_products
promotion_id :integer
product_id :integer
table promotion_afflicted_products
promotion_id :integer
product_id :integer
table promotion_afflicted_categories
promotion_id :integer
category_id :integer
然后我有一个固定装置如下:
#promotions.yml
cross_sell_1:
products: plumeboom_1, plumeboom_2
value: 100
value_type: percentage
type: CrossSellPromotion
#products.yml
plumeboom_1:
model: plumeboom1
url: plumeboom-1
plumeboom_2:
model: plumeboom2
url: plumeboom-2
plumeboom_3:
model: plumeboom3
url: plumeboom-3
当我运行我的单元测试时,它返回此错误消息:
1) Error:
test_the_truth(CartTest):
NoMethodError: undefined method `singularize' for :promotions_products:Symbol
如果您碰巧有类似的经历或知道如何解决这个问题,或者至少只是您认为可能是错误的地方,请告诉我,我准备好尝试一切!
非常感谢,请不要犹豫回复...这里真的很绝望!
最佳答案
答案是......你不能!
遗憾的是,有时 Rails 无法解决问题。在这些情况下,只需为关联表创建另一个 Fixture。
例如在本例中为 promotions_products.yml 并在其中输入:
pp1:
promotion_id: <%= Fixtures.identify(:cross_sell_1) %>
product_id: <%= Fixtures.identify(:plumeboom_1) %>
我设法在 fixtures 中使用 HABTM 连接,但是当它们也有 STI(单表继承)时,rails 似乎有问题。
关于ruby-on-rails - 在单元测试中使用 HABTM Fixture 的 Rails 单表继承返回 NoMethodError : undefined method `singularize' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2369895/
假设我正在构建一个拥有许多博客和用户的引擎。用户可以拥有多个博客,博客也可以拥有多个用户(用户 hasAndBelongsToMany blogs)。我将创建一个名为 users_blogs 的数据透
我正在尝试使用以下结构保存数据: 如您所见,用户和体验表之间存在 HABTM 关联。另一个 HABTM 位于 experiences_users 和 tags 之间。我创建了以下表单: create(
我有两张 table ,questions和 tags ,具有 HABTM 关系。添加问题时,我希望能够为问题指定一个标签(这只是第一个标签,以后可以添加更多标签)。标签是从他们的 table 上拉出
我有一个 HABTM 关联,但无法将数据保存到连接表中。 这是我的 Controller : public function create($id) { if (!i
之前问过,但仍然蒙在鼓里。 cakePHP HABTM, am I getting it all wrong? 会再试一次: 我有两个表:stores 和 managers 我想创建一个新表,mana
您好,我有一个用 cakephp 开发的网站。我有两张 table :成分属性 表之间的关系是 HasAndBelongsToMany(HABTM)。这是我的模型: class Ingredient
我正在尝试保存订单和订单中的产品。 正在保存订单,但没有保存产品。 我有一个 orders 表和一个 products 表以及一个 orders_products 表。 在订单模型中,我设置了 $ha
我目前有以下 SQL 数据库: CREATE TABLE `users` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(40),
我正在尝试保存订单和订单中的产品。 正在保存订单,但没有保存产品。 我有一个 orders 表和一个 products 表以及一个 orders_products 表。 在订单模型中,我设置了 $ha
我在用户和位置之间建立了 HABTM 关系。两种型号都有相应的 $hasAndBelongsToMany变量集。 当我管理用户位置时,我想删除用户和位置之间的关联,而不是位置。显然这个位置可能属于其他
序言:几天前我请 question 解决 HABTM 过滤器,即使有教程我也无法做到,所以“Obi Kwan Kenobi 你是我唯一的希望”。 我想要实现的目标:通过在 StaffStaffgrou
我正在自学 Rails,作为一个测试项目,我正在模拟一个类似于 stackoverflow 的简单问答应用程序。 在我的简化版本中,我有: 问题 答案 用户(问题和答案的作者) 我知道答案属于问题。
我正在尝试连接我拥有的两个连接表的值并根据条件关系显示结果...但我遇到了一些问题 我有一个用户模型(:name, :password, :email),事件模型(:name, :etc) 和兴趣模型
rails 3.2.3, ruby 1.9.3p194 尝试用 HABTM 相关子项目保存一个项目,我覆盖默认生成的 SQL,调用母项目和每个相关子项目的名称属性。如何正确指定关联的子项目名称? #{
到目前为止,我当前的 HABTM 实现与 :has_and_belongs_to_many 一起工作。 Notification 和 Privacy 都继承自 Preference。偏好是 STI。我
我有两个具有HABTM关系的模型-用户和角色。 用户-has_and_belongs_to_many:roles 角色-属于:user 我想在联接(users_roles表)中添加一个唯一性约束,该约
是否可以在 Rails 3 中的 habtm 关系上使用计数器缓存? 我真的需要它来加快我的申请速度。 最佳答案 HABTM 没有内置方法,但创建自己的计数器缓存很简单(实际上,在发明 counter
我有一个用户表和一个报告表,通过 HABTM 关系连接。 我可以保存在 reports_users 表中创建正确记录的报告,我可以使用 $this->User->ReportsUser->.. 查看关
我有两个非常简单的类: class Contact include Mongoid::Document has_and_belongs_to_many :organizations,
我有两个模型,帖子和标签。发布 HasAndBelongsToMany 标签。 假设 Post 1 具有标签 world、news 和 paper。 现在在连接表中,我想删除标签“paper”和Pos
我是一名优秀的程序员,十分优秀!