Mysql Rails中的关联

Mysql Rails中的关联,mysql,ruby-on-rails,Mysql,Ruby On Rails,我是rails新手,在理解关联的正确用法方面有点困难,因此在让代码正常工作方面遇到了问题。我已经看过rubyonrails.org,但仍然不确定 我有3个表(mySQL),一个包含产品,一个用于销售这些产品的零售商,另一个用于将产品链接到零售商并提供价格。一个零售商可以储存许多产品,一个产品可以被许多零售商出售。出于这个原因,我选择了一种“有很多,属于很多”的方法 我的API将返回json,其中包含产品的详细信息+销售它的零售商+零售商销售它的价格 当我运行代码时,出现以下错误: “在Produ

我是rails新手,在理解关联的正确用法方面有点困难,因此在让代码正常工作方面遇到了问题。我已经看过rubyonrails.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
class ProductPrice
控制器:

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
模块Api
模块V1
类ProductDetailsController
迁移文件:

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
class
有几个问题:

  • 鉴于我试图实现的情景,我是否使用了正确的
    联想
  • 我需要做哪些更改来解决错误

    谢谢

在“Rails 4路”中,作者提到了HABTM协会,但也很快指出,许多Rails开发人员认为它实际上已经过时了。我会将这些更改为简单的
有许多关联,并在您的
产品价格
模型中添加
属于

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

class Products < ActiveRecord::Base
class产品
致:

类产品
您需要在db中创建一个联接表,以建立一个has\u和\u-belish\u-to\u-many关系。我完全同意HABTM没有用处,特别是从长远来看。您好,Steve,我做了您上面建议的更改,但是它只返回表中的结果,而不是所有匹配的细节。如果我这样做。选择(“*”),这是可行的,但我知道这是一个糟糕的做法-你能建议一个替代方案吗?请确保你的代码遵循下面Ilan发布的内容。如果它仍然不起作用,最好用当前代码和新问题发布一个新问题。否则,该线程将变得非常混乱。
create_table :product_retailers do |t|
  t.float :price, null: false
  t.references :product, null: false
  t.references :retailer, null: false
end