简体   繁体   English

在Rails应用程序中定义模型的常用方法是什么?

[英]What is the common way to define models in Rails application?

All of the tutorials I've seen so far for RoR have shown me generating models like: 到目前为止我为RoR看到的所有教程都向我展示了生成如下模型:

rails generate User name:string placeofbirth:string

This generates a class for the model, and only actually references an attribute if I apply a validation of some kind. 这会为模型生成一个类,如果我应用某种验证,则只实际引用一个属性。

So my question is, how do I use a 'code' first approach when creating my models. 所以我的问题是,在创建模型时如何使用“代码”第一种方法。 Or is it the rails way to just right down on paper the attributes you want, run the generate command with each attribute you want and it's type, then run the rake db:migrate command? 或者它是直接在纸上找到你想要的属性的轨道方式,运行带有你想要的每个属性的generate命令和它的类型,然后运行rake db:migrate命令?

I'd love some more proven patterns on this subject because so far the way I've seen seems too empty. 我喜欢这个主题的一些更有效的模式,因为到目前为止我看到的方式似乎太空了。

Yes, this is the rails way- migration comes first and generates the code and the database- and the model class inspects the database to see what fields are there and make accessible via methods. 是的,这是rails方式 - 迁移首先出现并生成代码和数据库 - 模型类检查数据库以查看哪些字段并通过方法访问。

You can do gem install annotate_models if you want to get some comments in your model class with the attribute names and types. 如果要在模型类中使用属​​性名称和类型获取一些注释,可以执行gem install annotate_models

See here for an example: https://github.com/ctran/annotate_models 请参阅此处以获取示例: https//github.com/ctran/annotate_models

Rails uses an active record pattern for models which basically means that a model object will automatically map each DB column to an attribute so you don't have to specify all attributes in the model. Rails对模型使用活动记录模式 ,这基本上意味着模型对象将自动将每个DB列映射到属性,因此您不必指定模型中的所有属性。 It's a feature, but I agree that it might not be perfect for everyone. 这是一个功能,但我同意它可能并不适合每个人。 If you're using Rails 3 it should be easy to use another ORM of your choice if ActiveRecord's approach doesn't suit you. 如果您正在使用Rails 3,如果ActiveRecord的方法不适合您,则应该很容易使用您选择的其他ORM。 Here are some alternative ORMs that you could use. 以下是您可以使用的一些替代ORM。

Usually when you are developing some database backed web application, you know the database design(name of the tables, name of the columns in those tables and associations between different tables) beforehand. 通常,当您开发一些数据库支持的Web应用程序时,您事先知道数据库设计(表的名称,这些表中的列的名称以及不同表之间的关联)。

Rails, as mentioned by maarons in his answer, uses Active Record pattern. 正如maarons在他的回答中所提到的,Rails使用Active Record模式。 Your models are classes that represent a table in your database, an instance of your model class a row in that table and different attributes of an object represent values under different columns in the same table. 您的模型是表示数据库中的表的类,模型类的实例是该表中的一行,对象的不同属性表示同一表中不同列下的值。

When you create a model, usually, you are creating a class that represents one of the tables in your database. 通常,在创建模型时,您将创建一个表示数据库中某个表的类。 And while you are creating a model, you are also going to create a table in your database. 在创建模型时,您还将在数据库中创建一个表。 That means knowing the name of the table and columns within that table. 这意味着知道该表中的表名和列名。

So to answer your question, you must know all the columns, required for the time being, that will be in your tables. 因此,要回答您的问题,您必须知道暂时需要的所有列,这些列将在您的表中。 And hence available as attribute methods for your model objects. 因此可用作模型对象的属性方法。 You specify these columns to added in the table in the migration generated by rails generator while generating this model. 在生成此模型时,您可以在rails生成器生成的迁移中指定要添加到表中的这些列。 This is what usually everyone does. 通常每个人都这样做。

You can take a code first approach by creating a class, without running the rails model generator,under app/models/ but not inheriting it from ActiveRecord::Base . 您可以通过在app/models/下创建类而不运行rails模型生成器来创建类,而不是从ActiveRecord::Base继承它。 As you move forward in your development, you can generate migrations by $ rails generate migration MigrationName and creating table and adding columns using [add_column][2] to that table as required. 随着您在开发过程中的进展,您可以通过$ rails generate migration MigrationName$ rails generate migration MigrationName并创建表,并根据需要使用[add_column][2]向该表添加列。 Once you have created a table for this model, you will have to inherit that model from ActiveRecord::Base so that you can get all the Rails magic in your application. 为此模型创建表后,您必须从ActiveRecord::Base继承该模型,以便您可以在应用程序中获得所有Rails魔法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM