传递其他参数Rails的生成模型? [英] Passing additional parameters to Rails Generate Model?

查看:103
本文介绍了传递其他参数Rails的生成模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Ruby和Rails和我的红宝石1.9.2运行的Rails 3。

I'm new to Ruby and Rails and I'm running Rails 3 on Ruby 1.9.2.

我想创建一个模型,所以我看了看<一href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column">documentation得到我想要的表定义,但我不知道我会怎样通过这轨生成模式?

I want to create a model, so I looked at the documentation to get the table definition that I want, but I wonder how I would pass this to rails generate model?

基本上,我想这样的:

title :string, :null => false
details :text, :limit => 30000, :null => false

不过,我只能指定列名和类型,而不是:空或:限

But I can only specify the column name and type, not :null or :limit.

我已经试过轨道模型生成文章(题目:串:空=&GT;假)(详细信息:文本,:上限=&GT; 30000,:空=&GT;假)但只是告诉我,(是一个意外的标记。

I've tried rails model generate Article (title:string, :null => false) (details:text, :limit=>30000, :null => false) but that just tells me that ( is an unexpected token.

如果我刚生成的模型标题:字符串的详细信息:文本,然后将article.rb文件是空的,presumably,因为它是从数据库中读取反正

If I just generate the model with title:string details:text then the article.rb file is empty, presumably because it's read from the DB anyway.

由于一切都在Rails的应该是一个),简单的和b)的魔法,我想知道如果我只是失去了一些东西,并必须通过像哈希生成模式?或者,我真的要手动编辑迁移.RB文件?

As everything in Rails is supposed to be a) simple and b) magic, I'm wondering if I'm just missing something and have to pass something like a hash to generate model? Or do I really have to manually edit the migration .rb file?

推荐答案

是的,你就必须手动编辑该迁移的文件。发电机只是提供了一个起点,它没有做的一切。

Yes you do have to manually edit the migration file for that. The generator just offers a starting point, it does not do everything.

在迁移文件中这样做是很容易的反正。

Doing this in the migration file is very easy anyway.

create_table :articles do |t|
  t.string :title, :null => false
  t.text   :details, :limit => 3000, :null => false
end

这篇关于传递其他参数Rails的生成模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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