【问题标题】:rails 4 belongs_to relation and associations results in NoMethodErrorrails 4 belongs_to 关系和关联导致 NoMethodError
【发布时间】:2014-03-07 02:41:25
【问题描述】:

我正在尝试解决我在 rails 4 中的 belongs_to 关系:我有三个表 - 一个 hardware 表、一个 entity 表和一个 replacement 表。 entities是一个视图:

db=> \d entities;
      View "public.entities"
   Column    |  Type   | Modifiers
-------------+---------+-----------
 id          | integer |
 device      | text    |
 entity_type | text    |
 serial      | text    |

我用迁移创建另外两个:

class CreateHardwares < ActiveRecord::Migration
  def change
    create_table :hardwares do |t|
      t.string :vendor
      t.string :model
      t.decimal :price
    end
  end
end

class CreateReplacements < ActiveRecord::Migration
  def change
    create_table :replacements do |t|
      t.belongs_to :entity
      t.belongs_to :hardware
      t.integer :quantity
      t.belongs_to :replacement_hardware
    end
  end
end

所以基本思想是replacement 指的是一个实体(如电脑)。它还保存其(当前)hardware 和实体的替换 hardware 的信息。

class Entity < ActiveRecord::Base
      self.primary_key = "id"
  has_one :replacement
end

class Hardware < ActiveRecord::Base
end

class Replacement < ActiveRecord::Base
  belongs_to :entity
  belongs_to :hardware
  belongs_to :replacement_hardware, class_name: "Hardware", :foreign_key => 'replacement_hardware_id'
end

所以我用一些东西填充表并尝试查询:

Replacement.includes(:entity).where(:id=>1)
  Replacement Load (0.7ms)  SELECT "replacements".* FROM "replacements"  WHERE "replacements"."id" = 1
  Entity Load (2.4ms)  SELECT "entities".* FROM "entities"  WHERE "entities"."id" IN (35692)
=> #<ActiveRecord::Relation [#<Replacement id: 1, entity_id: 35692, entity_type: nil, hardware_id: nil, quantity: nil, replacement_hardware_id: nil, created_at: nil, updated_at: nil>]>

好的,看起来不错.. 但是,当我尝试访问关联 entity 时,它会引发错误:

Replacement.includes(:entity).where(:id=>1).entity
NoMethodError:   Replacement Load (0.6ms)  SELECT "replacements".* FROM "replacements"  WHERE "replacements"."id" = 1
  Entity Load (3.8ms)  SELECT "entities".* FROM "entities"  WHERE "entities"."id" IN (35692)
undefined method `entity' for #<Replacement::ActiveRecord_Relation:0x007fb156f6e758>

【问题讨论】:

    标签: model ruby-on-rails-4 foreign-keys belongs-to


    【解决方案1】:

    我认为您应该阅读有关关联的更多信息。如果您定义了 belongs_to 关系,您还应该将相应的 id 添加到您的迁移中。

    例如,如果替代品属于实体,则您的替代品表中应该有 entity_id。否则替换不知道它的关联。完成后,您实际上可以调用关系。

    一些附加说明:您不必将 id 定义为实体中的主键。坚持约定,仅在主键与默认值不同时才定义主键。您的实体模型也没有定义任何关系。关系是双向的,应该这样定义。

    【讨论】:

    • 从 postgres 输出中,我的 replacements 表中确实有 entity_id (将输出放在这些 cmets 中有点困难)。另外,belongs_to 迁移语句不是为我创建的吗?
    • 如果您之后定义关系,则不会。无论如何,更新我的答案以提供更多信息。
    【解决方案2】:

    呃!只是为了回答我自己的问题,以便我可以再次搜索它(以防我愚蠢到重复我的错误)......它基本上归结为where 声明。由于where 返回一个对象数组,它当然不知道entity 是什么。所以把它放在一个循环中或做first 将按预期工作!即

    Replacement.includes(:entity).where(:id=>1).first.entity
    

    Replacement.includes(:entity).where(:id=>1) do |r|
        r.entity
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多