【问题标题】:Rails ActiveRecord - retrieve data from association and relation errorRails ActiveRecord - 从关联和关系错误中检索数据
【发布时间】:2015-06-03 13:18:30
【问题描述】:

我正在学习活动模型,并且正在尝试检索关联。

我的模型是:

class Composition < ActiveRecord::Base
  has_and_belongs_to_many :materials
  belongs_to :product
end



class Material < ActiveRecord::Base
 has_and_belongs_to_many :compositions
 has_many :products, through: :composition
end

class Product < ActiveRecord::Base
  has_many :compositions
  has_many :materials, through: :composition
  accepts_nested_attributes_for :materials
end

我的架构是

  create_table "compositions", force: :cascade do |t|
    t.integer  "product_id"
    t.integer  "material_id"
    t.integer  "material_quantity"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
  end


  create_table "materials", force: :cascade do |t|
    t.string   "name"
    t.decimal  "unit_cost"
    t.string   "unit_measure"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end


  create_table "products", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

在我的 CompositionsController 索引方法中,我想检索产品 ID 的所有原材料。

路线是:

  resources :products do
    resources :compositions
  end

我现在拥有的是:

  def index
    @product = Product.find(params[:product_id])
    @compositions = @product.compositions
  end

如何从@compositions 中检索材质属性?

当我尝试使用时

@compositions = @product.compositions.includes(:materials)

它给了我错误:

PG::UndefinedTable: ERROR:  relation "compositions_materials" does not exist
LINE 5:                WHERE a.attrelid = '"compositions_materials"'...
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"compositions_materials"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

我想我错过了一些东西。 有什么提示吗?

提前致谢, 莱安德罗

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord nested-attributes ruby-on-rails-4.2


    【解决方案1】:

    我最终将模型关联更改为仅使用 has_many throughbelongs_to

    class Product < ActiveRecord::Base
      has_many :compositions
      has_many :materials, through: :composition
    end
    
    class Material < ActiveRecord::Base
     has_many :compositions
     has_many :products, through: :composition
    end
    
    class Composition < ActiveRecord::Base
      belongs_to :material, :class_name => "Material", :foreign_key => 'material_id'
      belongs_to :product
    end
    

    我正在使用这个查询来检索主要结果中的属性:

    @product = Product.find(params[:product_id])
    
    @compositions = @product.compositions.includes(:material).references(:materials)
    

    【讨论】:

    • belongs_to :material, :class_name =&gt; "Material", :foreign_key =&gt; 'material_id' belongs_to :material;如果您仍然遵守约定,请使用较短的方式。
    【解决方案2】:

    当您使用 has_and_belongs_to_many 关联时,您应该拥有带有 composition_idmaterial_id 列(均为整数)的 compositions_materials 表。

    更多关于这种关联的信息在这里:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

    【讨论】:

    • 组合表上的 ID 不会作为 ID 吗?我应该明确声明吗?组合表上的选择没有返回:id |产品编号 | material_id |材料数量 | created_at | updated_at ----+------------+-------------+------ --+----------------------------+------------------ ---------- 5 | 1 | 1 | 3 | 2015-03-30 05:20:27.385293 | 2015-03-30 05:20:27.385293
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多