使用 rails 4,我想在一个地方对我的整个数据模型进行建模。rails generate scaffold
据我了解,通常通过迁移或使用命令以及模型文件中的类之间的关联来生成每个类的字段。这感觉有点麻烦,我想知道是否没有办法将所有内容集中在一个地方。我想知道是否可以在模型文件中进行字段定义。这将使您能够在一个地方查看和编辑整个模型 - 对于引导具有不少关联的新应用程序非常有用且紧凑。
2 回答
这取决于你在说什么,具体来说。直接回答你的问题
我想知道是否可以在模型文件中添加字段定义
不幸的是,没有这样的方法(至少我知道没有)可以在您的ActiveRecord::Base
模型和数据库之间进行同步。简而言之,这种形式的同步是通过 rails 迁移文件完成的。
使用该rails g scaffold
命令的方便之处在于它会同时为您创建迁移文件以及控制器、模型和测试类。但是,您绝对不需要通过脚手架执行任何此操作;如果你愿意,你可以一块一块地做。如果在描述字段时出错或需要修改某些内容,您只需编写/生成一个新的迁移文件。
从技术上讲,您可以在模型中编写您想要的任何内容,但它不会反映在数据库中,除非您 (A) 手动更改数据库(坏主意),或 (B) 编写或生成迁移文件来更改数据库根据需要(更好的主意)。
基本上,要按照您的想法进行操作,您通常需要编写/生成迁移。如您所知,迁移本质上是一个以某种方式更改数据库的脚本:添加或删除列、重命名列、创建表等。
除了关联之外,模型中没有写入任何字段信息。当您从数据库加载记录时,ActiveRecord(基本上)使用适当的访问器/修改器构建对象。简而言之,在数据库中为该表/模型定义的任何列都是您的模型对象将包含的内容。
关于您关于生成脚手架时关联很麻烦的注释,如果您指的是 Rails 如何仅belongs_to
在指定模型中编写关系的一部分,是的,这可能有点烦人,但我相信原因是仅仅因为关系的那一方是唯一可以准确假设的一方;Rails 不知道你最终想要建立什么样的关系。因此,您确实需要在相关模型中指出关系的“另一端”(has_many
例如,侧面)。
希望我的漫谈有帮助。
编辑
In regards to your question in the comments, I can't really think of any "shortcuts" or workflow suggestions at the moment. Honestly, I think the best workflow in this case is to try to make sure you have the model as "correct" as possible before you write anything.
Think of it this way: in a framework that can synchronize the model with the DB, you're basically doing the same thing as migrations anyways because they would have to execute the same kind of DB commands (alter table
and such). The only difference is that (A) the Rails migrations are acting as a record of each change, and (B) the specifics of the migration are explicitly indicated. Not to mention, you have the freedom to do a heck of a lot more in the migration, or add some decision logic, etc.
In general, though it may look really huge and cumbersome, having a lot of migrations isn't a bad thing, necessarily. In fact, I'd even say its expected (especially for longer-lived systems). As I said, it shows a record of what happened along the way, meaning how the system evolved and changed. Additionally, they're useful when you need to deploy a change that should be picked up by other developers, and/or executed in production.
Just a note, if you're deploying for the first time in production, one common thing to do is to execute 'rake db:schema:load' to load the entire schema at once (no data, just the table structure). This tends to be much faster than running each and every migration. This is only for a first-time installations, though.
Another note, the above paragraph is also the reason why you don't want to put any seed information (meaning, don't create records) in the migration files. That's what your seed.rb file is for.
So in conclusion, I think the main point you can take away from this is that migrations are very much akin to the commit history of your version control system (GIT, SVN, etc). I don't know about you, but my commit history does have a lot of "Fixing bug A", and "Correcting bug fix from previous commit" etc, and though its weird, this is technically perfectly acceptable and perhaps somewhat expected in most cases.
通常,您将在模型中定义关联并通过迁移管理模型的数据库字段。脚手架不是建立模型之间关联的正确解决方案。脚手架将构建模型、视图和控制器,所以有时它可能是矫枉过正。更不用说,当您使用脚手架时,您生成的每个脚手架都会获得完整的 CRUD。它通常会比您需要的更多。