【问题标题】:Association in RailsRails 协会
【发布时间】:2015-04-28 18:41:36
【问题描述】:

我是 Rails 新手,在理解关联的正确用法方面有些困难,因此在让我的代码正常工作时遇到了问题。我已经查看了 ruby​​onrails.org,但仍然不确定。

我有 3 张表 (mySQL),一张将包含产品,一张用于销售这些产品的零售商,一张将产品链接到零售商并有价格。一个零售商可以储存许多产品,一个产品可以由许多零售商销售。出于这个原因,我采用了“拥有许多并属于许多”的方法。

我的 API 将返回 json,其中包含有关产品的详细信息 + 销售产品的零售商 + 零售商的销售价格。

当我运行我的代码时,我收到以下错误:
“在 ProductPrice 上找不到名为 'products' 的关联;也许你拼错了?”

我的模特:

    class ProductPrice < ActiveRecord::Base
    end

    class Products < ActiveRecord::Base
         has_and_belongs_to_many :productprices
    end

    class Retailer < ActiveRecord::Base
         has_and_belongs_to_many :productprices
    end

控制器:

module Api
    module V1
        class ProductDetailsController < ApplicationController          
            def show
                @prod = ProductPrice.joins(:products, :retailers)
                render json: @prod
            end 
        end
    end
end

迁移文件:

class CreateRetailers < ActiveRecord::Migration
  def change
    create_table :retailers do |t|
      t.string :name
      t.string :url
      t.string :aff_id
      t.string :img_url

      t.timestamps
    end
  end
end

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
        t.string :name
        t.string :brand
        t.string :category
        t.string :sub_cat
        t.string :nut_cal
        t.string :nut_pro
        t.string :nut_fat
        t.string :nut_sat_fat
        t.string :nut_carb
        t.string :nut_sugar
        t.string :nut_salt
        t.text :ingredients
        t.string :flavours
        t.text :description
        t.string :img_url

      t.timestamps
    end
  end
end

class CreateProductPrices < ActiveRecord::Migration
  def change
    create_table :product_prices do |t|
        t.belongs_to :product, index: true 
        t.belongs_to :retailer, index: true
        t.string :price
        t.string :aff_link

      t.timestamps
    end
  end
end

几个问题:

  • 鉴于我试图实现的场景,我是否使用了正确的
    协会?
  • 我需要进行哪些更改才能解决错误?

    谢谢。

【问题讨论】:

  • 你需要在数据库中创建一个连接表才能拥有has_and_belongs_to_many关系。

标签: mysql ruby-on-rails


【解决方案1】:

在“The Rails 4 Way”中,作者提到了 HABTM 协会,但很快也说许多 Rails 开发人员认为它实际上已经过时了。我会将这些更改为简单的 has_many 关联并在您的 ProductPrices 模型中添加 belongs_to

此外,按照惯例,模型应该是单数的。所以,我会改变:

class Products < ActiveRecord::Base

到:

class Product < ActiveRecord::Base

【讨论】:

  • 我非常同意 HABTM 没有用处,尤其是从长远来看。
  • 嗨史蒂夫,我做了你上面建议的更改,但是它只返回表中的结果,而不是所有匹配的细节。如果我这样做 .select("*") 这行得通,但我知道这是不好的做法 - 你能建议一个替代方案吗?
  • 确保您的代码遵循 Ilan 在下面发布的内容。如果它仍然不起作用,最好用您当前的代码和新问题发布一个新问题。否则,这个线程会变得很混乱。
【解决方案2】:
   class Product < ActiveRecord::Base
     has_many :product_retailers, dependent: :destroy, inverse_of: :product
     has_many :retailers, through: :project_retailers   
   end

   class Retailer < ActiveRecord::Base
      has_many :product_retailers, dependent: :destroy, inverse_of: :retailer
      has_many :products, through: :product_retailers     
    end

   class ProductRetailer < ActiveRecord::Base
     belongs_to :product
     belongs_to :retailer
     validates_presence_of :price, :product, :retailer
     validates_numeracality_of :price, greater_than: 0
   end

迁移

create_table :product_retailers do |t|
  t.float :price, null: false
  t.references :product, null: false
  t.references :retailer, null: false
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多