Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql (子)类别的表和Ruby ActiveRecord类设计_Sql_Ruby On Rails_Ruby_Database_Activerecord - Fatal编程技术网

Sql (子)类别的表和Ruby ActiveRecord类设计

Sql (子)类别的表和Ruby ActiveRecord类设计,sql,ruby-on-rails,ruby,database,activerecord,Sql,Ruby On Rails,Ruby,Database,Activerecord,我想我有一个相当简单的问题,因为我是Ruby新手,甚至是ActiveRecords新手 我想要实现的是使用ActiveRecords(以及相应的SQL模式)的类表示,它对以下问题进行建模: 存在类别和子类别(由父项id建模) 产品只属于一个类别 每个产品都可以有0..inf功能 功能只是有一些数据字段,仅由产品引用 我的当前架构如下图所示: 这个模式适合ActiveRecords吗?这些课程看起来怎么样?我简直搞不懂JoinTable如何适合ActiveRecord结构 此外,如何从par

我想我有一个相当简单的问题,因为我是Ruby新手,甚至是ActiveRecords新手

我想要实现的是使用ActiveRecords(以及相应的SQL模式)的类表示,它对以下问题进行建模:

  • 存在类别和子类别(由父项id建模)
  • 产品只属于一个类别
  • 每个产品都可以有0..inf功能
  • 功能只是有一些数据字段,仅由产品引用
我的当前架构如下图所示:

这个模式适合ActiveRecords吗?这些课程看起来怎么样?我简直搞不懂JoinTable如何适合ActiveRecord结构

此外,如何从
parent\u id->categories.id
对链接建模

感谢您的帮助

干杯

以下是

这里有很多不同关系的例子,以及如何构建它们。花点时间来理解你如何/为什么构建这些关联是值得的

对于多对多的加入,您将希望看到

  • 有很多…,:通过=>…
  • 拥有并且属于许多…

这些文档解释了何时以及为什么要使用每种产品。

我的型号如下所示:

class Category < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :category
  has_many   :product_features
  has_many   :features, :through => :product_features
end

class ProductFeature  < ActiveRecord::Base
  belongs_to :product
  belongs_to :feature
end

class Feature < ActiveRecord::Base
  has_many   :product_features
  has_many   :products, :through => :product_features
end

要对您描述的关系建模,您需要执行以下操作:

models/category.rb
class Category < ActiveRecord::Base
    has_many :products
    has_many :subcategories, :class_name => "Category", :foreign_key => :parent_id
end

models/product.rb
class Product < ActiveRecord::Base
    belongs_to :product
    has_many :features, :through => :product_features
    has_many :product_features
end

models/feature.rb
class Feature < ActiveRecord::Base
  has_many :product_features
  has_many :products, :through => :product_features
end

models/productfeature.rb
class ProductFeature < ActiveRecord::Base
  belongs_to :product
  belongs_to :feature
end
链接的遍历是通过您在模型中设置的关系进行的,目的是在使用合理名称的上下文中使其可读。根据您的问题,self-join也包含在一个很好的示例中。本指南的其余部分还详细介绍了其他关系


另一件需要记住的事情是创建迁移,以便使用作为外键的id创建联接表。

谢谢,现在您的答案包含实际信息:-)就我现在所见,这意味着我根本不必对联接表建模。但是映射程序如何知道我的联接表的名称呢?我可以指定它吗?如果您阅读API文档,会有很多名称的推断(记住rails是
约定优于配置
)。这就是为什么这样的工作在你开始时会让你绊倒的原因——我最近一直在努力寻找自我参照的多对多的连接,以获取广泛的信息。这正是我想要的。当我仔细阅读你和格兰特的优秀答案后,我将很快接受答案。由于答案最完整,我选择了你的答案作为最佳答案。这让我走上了正确的道路,我想我现在已经掌握了窍门。类别仍然缺少一个
属于:parant\u类别,:class\u name=>“Category”
以允许上下遍历。但除此之外,一切似乎都是正确的。再次感谢!
models/category.rb
class Category < ActiveRecord::Base
    has_many :products
    has_many :subcategories, :class_name => "Category", :foreign_key => :parent_id
end

models/product.rb
class Product < ActiveRecord::Base
    belongs_to :product
    has_many :features, :through => :product_features
    has_many :product_features
end

models/feature.rb
class Feature < ActiveRecord::Base
  has_many :product_features
  has_many :products, :through => :product_features
end

models/productfeature.rb
class ProductFeature < ActiveRecord::Base
  belongs_to :product
  belongs_to :feature
end
@category = Category.first   #get the first category
@category.subcategories      #returns an array of categories