通过 ruby​​onrails 中的脚手架设置对表的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13116761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:56:35  来源:igfitidea点击:

Setting references to a table through scaffolding in rubyonrails

ruby-on-railsruby

提问by Niths

I am now doing a project on ruby on rails. I created a entity named product and i want to set a many to many relation to other entity named category.

我现在正在做一个关于 ruby​​ on rails 的项目。我创建了一个名为 product 的实体,我想设置与其他名为 category 的实体的多对多关系。

script/generate scaffold product prd_name:string category:references 

By doing this code only one to one mapping is possible.How can i set many to many without hard coding?

通过执行此代码,只能进行一对一映射。如何在没有硬编码的情况下设置多对多?

采纳答案by Niths

We can not do this through scaffolding. We must edit the model of the class to set many to many relationship.

我们不能通过脚手架来做到这一点。我们必须编辑类的模型以设置多对多关系。

回答by noodl

You should not expect to be able to generate your app via scaffolding alone. It is meant only to provide an example for getting started.

您不应该期望能够仅通过脚手架来生成您的应用程序。它仅用于提供入门示例。

The most flexible kind of many-to-many relationship in rails is called has many through. This requires a join table which would typically be called 'categorisations' in this case. It would need a product_idcolumn declared as belongs to :productand a category_idcolumn declared as belongs_to :category. The three models (including the join model) would be declared thus:

rails 中最灵活的多对多关系称为has many through。这需要一个连接表,在这种情况下通常称为“分类”。它需要一个product_id声明为belongs to :productcategory_id列和一个声明为的列belongs_to :category。三个模型(包括连接模型)将被声明为:

# Table name: products
# Columns:
#   name:string

class Product < ActiveRecord::Base
  has_many :categorisations, dependent: :destroy
  has_many :categories, through: :categorisations
end

# Table name: categories
# Columns:
#   name:string

class Category < ActiveRecord::Base
  has_many :categorisations, dependent: :destroy
  has_many :products, through: :categorisations
end

# Table name: categorisations
# Columns:
#   product_id:integer
#   category_id:integer

class Categorisation < ActiveRecord::Base
  belongs_to :product
  belongs_to :category
end

Note that I've named the columns namerather than prd_namesince this is both human-readable and avoids redundant repetition of the table name. This is highly recommended when using rails.

请注意,我已经命名了列,name而不是prd_name因为这既是人类可读的,又避免了表名的冗余重复。在使用 rails 时强烈建议这样做。

The models can be generated like this:

模型可以这样生成:

rails generate model product name
rails generate model category name
rails generate model categorisation product:references category:references

As for generating the scaffolding, you could replace modelwith scaffoldin the first two commands. Again though, I don't recommend it except as a way to see an example to learn from.

至于生成脚手架,你可以替换前两个命令中的modelwith scaffold。尽管如此,我不推荐它,除非作为一种查看示例的方式来学习。

回答by Rui Castro

It's possible to generate a model with references with a command like this

可以使用这样的命令生成带有参考的模型

$ rails generate model Comment commenter:string body:text post:references

See http://guides.rubyonrails.org/getting_started.html#generating-a-model

请参阅http://guides.rubyonrails.org/getting_started.html#generating-a-model

回答by user3402754

It's now possible to generate a scaffold with references with a command like this

现在可以使用这样的命令生成带有引用的脚手架

$ rails generate scaffold Comment commenter:string body:text post:references