怎样才能添加到模型的属性? [英] How does one add an attribute to a model?

查看:132
本文介绍了怎样才能添加到模型的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在铁轨我生成两个字符串的模式,并希望增加更多。我怎么会去这样做呢?

In rails I generate a model with two strings and would like to add more. How would I go about doing this?

推荐答案

活动记录您的表中的列映射到模型中的属性,所以你不需要告诉你需要更多的轨道,你真正需要的是创造多列和导轨检测到它们,并添加属性:

Active Record maps your tables columns to attributes in your model, so you don't need to tell rails that you need more, what you really need is to create more columns and rails detect them and add the attributes:

您可以通过迁移到表中添加更多的列

rails generate migration AddNewColumnToMyTable column_name:column_type(string by default)

这会生成一个文件:

db/2011.....rb

打开它,并添加您需要的列:

Open it and add the columns you need:

self.up
  add_column :tablename, :column_name, :column_type
end

希望这有助于。

我假设你需要存储这种属性数据库,但也许你只需要一个虚拟属性。

I'm assuming you need to store this attributes on database, but maybe you just need a Virtual Attribute.

这篇关于怎样才能添加到模型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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