如何在 ActiveRecords 中创建对 Ruby 中对象的引用?

发布于 2024-12-08 09:00:11 字数 563 浏览 0 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-12-15 09:00:11

如果我理解正确的话,你正在寻找关联。这是很棒的指南
你应该阅读。这里需要理解的是,您可以使用该指南中描述的一系列方法在模型中定义它们如何相互关联。

我建议您这样做:

rails generate scaffold Car <db columns>
rails generate model Engine valves:integer capacity:integer rpm:integer car_id:integer

在您的两个模型中:

class Car < ActiveRecord::Base
  has_one :engine
end 

class Engine < ActiveRecord::Base
  belongs_to :car
end

您实际上可以为两个模型生成脚手架......这将创建控制器和视图。但在这种情况下,添加

accepts_nested_attribues_for :engine

到您的汽车模型可能是有意义的。这将允许您从控制器和汽车模型的视图管理引擎模型的操作。

无论如何,我希望这可以帮助您开始找到您需要的东西。

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:

rails generate scaffold Car <db columns>
rails generate model Engine valves:integer capacity:integer rpm:integer car_id:integer

In your two models:

class Car < ActiveRecord::Base
  has_one :engine
end 

class Engine < ActiveRecord::Base
  belongs_to :car
end

You can actually generate scaffold for both models...that will create controller and views. But in this case it might make sense to add

accepts_nested_attribues_for :engine

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.

物价感观 2024-12-15 09:00:11

您可以使用 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 file

has_many :engines in your Car model file

belongs_to :car in your Engine model file

Don't forget to check the rails api for options (default, relation callbacks...)(here for exemple : http://railsapi.com/doc/rails-v3.0.8rc1/)

星光不落少年眉 2024-12-15 09:00:11

你应该更多地了解 Ruby。 Ruby 不是静态语言,这意味着每个变量都可以保存每种类型的对象。

Rails生成命令使用valve:integer等仅用于数据库目的,因为数据库需要此信息。

关于您的关系问题,您应该阅读有关 has_many、bleongs_to 等的内容(请参阅 http:// api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)在Rails中,您可以像这样定义您的关系

class Car
  belongs_to :engine
end

class Engine
  has_many :cars
end

此外,您还必须添加一个外部汽车的关键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

class Car
  belongs_to :engine
end

class Engine
  has_many :cars
end

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.

來不及說愛妳 2024-12-15 09:00:11

关系没有脚手架,你必须学习如何“手工”完成(这并不太要求)。查看“Rails 指南”,这里"Active Record Association"

在您的示例中,您必须执行以下步骤:

  1. 创建迁移以迁移数据库:rails g migration AddIds
  2. 修改迁移以包含您必须拥有的附加 ID:

    <前><代码>...
    添加列:引擎、:汽车 ID、:整数

  3. 向您的模型添加以下代码:

    类汽车
      有_一个:引擎
      ...
    结尾
    发动机类
      所属:汽车
      ...
    结尾
    

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:

  1. Create a migration to migrate the database: rails g migration AddIds
  2. Modify the migration to include the additional ID you have to have:

    ...
    add_column :engines, :car_id, :integer
    
  3. Add to you models the following code:

    class Car
      has_one :engine
      ...
    end
    class Engine
      belongs_to :car
      ...
    end
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文