如何添加额外的列到用户表与rails和devise宝石? [英] How can I add additional columns to a Users table with rails and devise gem?

查看:179
本文介绍了如何添加额外的列到用户表与rails和devise宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在rails中制作一个应用程序,以便当用户添加他们的药物,它将添加药物在用户表中的新列。

I am making an app in rails so that when a User adds their medications, it will add the medication in a new column in the Users table.

我是使用devise gem和我的用户模型,我没有对用户表进行任何额外的更改,除了在运行 rails generate devise User 时创建它。如何将新药物列添加到用户表?如何更新表单中的属性?

I am using the devise gem with my User model, and I haven't made any additional changes to the User table other than creating it when running rails generate devise User. How do I add the new medications column to the User table? And how do I update that attribute in a form?

推荐答案



Ok so there are two ways of going about this.

这是可扩展的,并且将支持具有多种药物的单个用户。

This is scalable and will support a single user having multiple medications.

您需要创建一个药物模型并将其与用户模型。意思是当用户添加/更新/删除药物时,它将添加与该用户相关联的药物对象。您可以通过几个简单的步骤acheiev这

You need to create a Medications model and associate it with the User model. Meaning that when a user adds/updates/deletes a medication it will add a Medication object that is associated with this User. You can acheiev this in just a few simple steps:

步骤1:创建药物模型,控制器,视图, / p>

Step 1: Create the Medication model, controllers, views, migration file by scaffolding

rails g scaffold medications name:string user_id:integer

这将创建您需要的所有文件,包括控制器,模型,路由,新的迁移文件,测试等。

This will create all the files you need including a controller, model, routes, a new migration file, tests, etc.

第2步:您可以打开新创建的迁移文件,看到这样的信息,我认为药物名称和user_id应该足以满足您的需要

Step 2: You can open the newly created migration file and see something like this, I think the medication name and the user_id should suffice for your needs

  def change
    create_table :medications do |t|
      t.string  :name
      t.integer :user_id

      t.timestamps
    end

    add_index :medications, :user_id
  end

步骤3:迁移数据库

rake db:migrate

第4步:关联模型
打开您的模型文件并放置这些关联

Step 4: Associate the models Open your model files and place these associations

Medication.rb

Medication.rb

belongs_to :user

User.rb

has_many :medications, dependent: :destroy




只需在您的用户模型中添加一个(或多个)药物列。这对于现实世界的应用是不实际的,你不应该在实践中这样做。但是如果你想这样做:


Just adding a new (or multiple) medications columns to your User model. This is not practical for a real world application, and you should not do it in practice. But if you wanted to you would do it as such:

rails g migration AddMedicationColumnToUser

在您的新迁移文件中添加列
def change
add_column:users,:medication,:string

In your new migration file add the column def change add_column :users, :medication, :string end

迁移数据库

rake db:migrate

请务必使用 attr_accessor:medication 更新您的模型if on rails 3 else将属性添加到控制器中的强参数中如果rails 4

Make sure you update your model with attr_accessor :medication if on rails 3 else add the attribute to your strong params in the controller if rails 4

这篇关于如何添加额外的列到用户表与rails和devise宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆