在Rails 5中使用模型中的默认值

时间:2021-01-20 13:18:20

I want to add some category for my Rails post app.

我想为我的Rails帖子应用添加一些类别。

I want them as fixed value so user can choose from the drop down menu the specific category. My migration file looks like this:

我希望它们是固定值,因此用户可以从下拉菜单中选择特定类别。我的迁移文件如下所示:

class CreateCategories < ActiveRecord::Migration[5.1]
  def change
    create_table :categories do |t|
      t.string :name
      t.timestamps
    end
  end
end

What do I have to do to add some fixed value in my categories model?

如何在我的类别模型中添加一些固定值?

2 个解决方案

#1


1  

You could set constant like NAMES = %w(category1 category2) inside Category model, add inclusion validation and get values for your dropdown like this: Category::NAMES. In this case don't forget to add database index. Obviously you will query posts related to some category.

您可以在Category模型中设置常量,如NAMES =%w(category1 category2),添加包含验证并获取下拉列表的值,如下所示:Category :: NAMES。在这种情况下,不要忘记添加数据库索引。显然,您将查询与某个类别相关的帖子。

There is another option though provided by ActiveRecord::Enum. It lets you declare category field right inside Post without Category model at all. If you don't need to manage categories outside the codebase (some admin panel), I would recommend this:

ActiveRecord :: Enum提供了另一种选择。它允许你在Post中声明类别字段,而没有Category模型。如果您不需要管理代码库之外的类别(某些管理面板),我建议您:

class Post < ActiveRecord::Base
  enum category: [:category1, :category2]
end

#2


0  

If you want to add some predefined categories, then this is called "seed data": Migrations and Seed data.

如果要添加一些预定义类别,则称为“种子数据”:迁移和种子数据。

To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy. This is especially useful when reloading the database frequently in development and test environments. It's easy to get started with this feature: just fill up db/seeds.rb with some Ruby code, and run rails db:seed:

为了在创建数据库后添加初始数据,Rails具有内置的“种子”功能,使得该过程快速简便。在开发和测试环境中频繁地重新加载数据库时,这尤其有用。这个功能很容易上手:只需用一些Ruby代码填充db / seeds.rb,然后运行rails db:seed:

Category.create(name: 'category 1')
Category.create(name: 'category 2')

#1


1  

You could set constant like NAMES = %w(category1 category2) inside Category model, add inclusion validation and get values for your dropdown like this: Category::NAMES. In this case don't forget to add database index. Obviously you will query posts related to some category.

您可以在Category模型中设置常量,如NAMES =%w(category1 category2),添加包含验证并获取下拉列表的值,如下所示:Category :: NAMES。在这种情况下,不要忘记添加数据库索引。显然,您将查询与某个类别相关的帖子。

There is another option though provided by ActiveRecord::Enum. It lets you declare category field right inside Post without Category model at all. If you don't need to manage categories outside the codebase (some admin panel), I would recommend this:

ActiveRecord :: Enum提供了另一种选择。它允许你在Post中声明类别字段,而没有Category模型。如果您不需要管理代码库之外的类别(某些管理面板),我建议您:

class Post < ActiveRecord::Base
  enum category: [:category1, :category2]
end

#2


0  

If you want to add some predefined categories, then this is called "seed data": Migrations and Seed data.

如果要添加一些预定义类别,则称为“种子数据”:迁移和种子数据。

To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy. This is especially useful when reloading the database frequently in development and test environments. It's easy to get started with this feature: just fill up db/seeds.rb with some Ruby code, and run rails db:seed:

为了在创建数据库后添加初始数据,Rails具有内置的“种子”功能,使得该过程快速简便。在开发和测试环境中频繁地重新加载数据库时,这尤其有用。这个功能很容易上手:只需用一些Ruby代码填充db / seeds.rb,然后运行rails db:seed:

Category.create(name: 'category 1')
Category.create(name: 'category 2')