为什么rails会生成空模型?(Why rails is generating empty models?)

编程入门 行业动态 更新时间:2024-10-22 21:24:16
为什么rails会生成空模型?(Why rails is generating empty models?)

我正试图生成一些模型,但它们正在生成没有属性。

我使用的是Linux系统,rails版本是:rails --version Rails 4.0.0

我试图用这个命令生成模型:

rails g scaffold Bsdsd description:string test:string oaso:integer

rails g model Asdsd description:string test:string oaso:integer

在这个空的类模型中的第一个结果一切正常:

class Bsdsd < ActiveRecord::Base end

第二个结果是测试文件,迁移文件(包含属性)和这个类模型:

class Asdsd < ActiveRecord::Base end

我怎样才能纠正这种行为?

I'm trying to generate some models but they are being generated without attributes.

I'm using a linux system and the rails version is: rails --version Rails 4.0.0

I've tried to generate the models using this commands:

rails g scaffold Bsdsd description:string test:string oaso:integer

and

rails g model Asdsd description:string test:string oaso:integer

The first results in this empty class model everything else ok:

class Bsdsd < ActiveRecord::Base end

The second results in test files, migrations file(that contains the attributes) and this class model:

class Asdsd < ActiveRecord::Base end

How can I correct this behavior?

最满意答案

模型属性是从数据库列中推断出来的,因此您不需要在模型类中指定它们。

在Rails 3.2中,如果我没有记错,

# attr_accessible :description, :test, :oaso

生成的线。 但受保护的属性在Rails 4.0中已被弃用,并被强参数机制所取代。

Model attributes are inferred from database columns, so you don't need them specified in model classes.

In Rails 3.2 you had (if I remember correctly)

# attr_accessible :description, :test, :oaso

line generated. But protected attributes are deprecated in Rails 4.0 and replaced by strong parameters mechanism.

更多推荐

本文发布于:2023-08-02 02:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   rails   generating   models   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!