如何在 ActiveRecords 中创建对 Ruby 中对象的引用?
我是一名 Java 开发人员,这几天我一直在学习 Rails。我有一个 Java EE 应用程序(使用 Hibernate for ORM),我正在尝试将其移植到 Rails。我使用脚手架来生成一些模型。但我还有其他模型,其中包含对其他模型的引用。我如何定义这些关系?我也可以这样做吗?
这是我正在尝试做的事情的一个例子。
public class Engine {
private int valves;
private int capacity;
private int rpm;
}
我可以通过执行以下操作在 ruby 中构建 Engine 类:
rails generate scaffold Engine valves:integer capacity:integer rpm:integer
这对我来说是棘手的部分:
public class Car {
private Engine engine;
}
如何在 Ruby 中构建 Car 类?
I am a Java developer I've been learning Rails for the past few days. I have a Java EE application (Uses Hibernate for ORM) that I am trying to port to Rails. I have used scaffolding to generate a few of my models. But I have other models which contain references to other models. How do I define the relations? Can I scaffold that as well?
Here is an example for what I am trying to do.
public class Engine {
private int valves;
private int capacity;
private int rpm;
}
I can scaffold the Engine class in ruby just by doing the following:
rails generate scaffold Engine valves:integer capacity:integer rpm:integer
Here is the tricky part for me:
public class Car {
private Engine engine;
}
How do I scaffold the Car class in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,你正在寻找关联。这是很棒的指南
你应该阅读。这里需要理解的是,您可以使用该指南中描述的一系列方法在模型中定义它们如何相互关联。
我建议您这样做:
在您的两个模型中:
您实际上可以为两个模型生成脚手架......这将创建控制器和视图。但在这种情况下,添加
到您的汽车模型可能是有意义的。这将允许您从控制器和汽车模型的视图管理引擎模型的操作。
无论如何,我希望这可以帮助您开始找到您需要的东西。
If I understand correctly you are looking for associations. Here's a great guide
that you should read. The thing to understand here is that you define in your models how the they relate to each other with a series of methods described in that guide.
Here is what I would suggest you do:
In your two models:
You can actually generate scaffold for both models...that will create controller and views. But in this case it might make sense to add
to your Car model instead. This will allow you to manage the manipulation of the Engine model from the controller and views of the Car model.
At any rate, I hope this helps you start to find what you need.
您可以使用 activerecord 迁移的
references
帮助器来完成此操作。<代码>
Rails 生成脚手架 汽车引擎:参考文献 ...
它将
在您的迁移文件中添加:
t.references :engine
has_many :engines
在您的汽车模型文件belongs_to :car
中你的引擎模型文件不要忘记检查 Rails api 的选项(默认、关系回调...)(此处为例:http://railsapi.com/doc/rails-v3.0.8rc1/)
You can do it using the
references
helper of activerecord migration.rails generate scaffold Car engine:references ...
it will add :
t.references :engine
in your migration filehas_many :engines
in your Car model filebelongs_to :car
in your Engine model fileDon't forget to check the rails api for options (default, relation callbacks...)(here for exemple : http://railsapi.com/doc/rails-v3.0.8rc1/)
你应该更多地了解 Ruby。 Ruby 不是静态语言,这意味着每个变量都可以保存每种类型的对象。
Rails生成命令使用valve:integer等仅用于数据库目的,因为数据库需要此信息。
关于您的关系问题,您应该阅读有关 has_many、bleongs_to 等的内容(请参阅 http:// api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)在Rails中,您可以像这样定义您的关系
此外,您还必须添加一个外部汽车的关键engine_id。
这是可行的,因为 Rails 中使用了多种约定。
如果没有基础教程,你将走不远。
You should learn more about Ruby. Ruby is not a static language, meaning every variable can hold every kind of object.
The rails generate command uses valves:integer etc. only for database purposes, because databases need this information.
Concerning your relations problem you should read about has_many, bleongs_to etc. (see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html) In Rails you would define your relation like this
Furthermore you have to add a foreign key engine_id to Car.
This works because there are several conventions used in Rails.
Without a basic tutorial you will not get far.
关系没有脚手架,你必须学习如何“手工”完成(这并不太要求)。查看“Rails 指南”,这里"Active Record Association"。
在您的示例中,您必须执行以下步骤:
rails g migration AddIds
修改迁移以包含您必须拥有的附加 ID:
<前><代码>...
添加列:引擎、:汽车 ID、:整数
向您的模型添加以下代码:
There is no scaffolding for relations, you have to learn how to do it "by hand" (which is not too demanding). Have a look at the "Rails Guides", and here "Active Record Association".
In your example, you have to do the following steps:
rails g migration AddIds
Modify the migration to include the additional ID you have to have:
Add to you models the following code: