我需要帮助形成正确的ActiveRecord这个协会正确、协会、ActiveRecord

2023-09-08 15:55:46 作者:博你怜爱

基本上,用户可以参与一个或多个事件,作为(或潜在的两者)一个供应商和作为事件的教师的成员。

有一个用户,在默认情况下,归类为既不是供应商还是一名教师,而是达到一个事件的情况下(例如,用户已经承认了事件作为内(或两者)状态(ES)其教师的成员)。

好像是我想说的是,用户通过(或两者)的供应商或教师链接表许多事件,但我不知道我会去的重新presenting这我的Rails模型。这是我到目前为止已经试过:

 类用户的LT;的ActiveRecord :: Base的
  的has_many:通过=&GT事件;厂商
  的has_many:通过=&GT事件;学院
结束
 

下面是我认为我需要进行查询的示例:

  SELECT * FROM厂商,其中user_ID的= 1;
SELECT * FROM教师,其中user_ID的= 1;
 
Java中的at com.jfinal.plugin.activerecord.Db.query错误,怎么解决呀

有人可以提供一些方向是如何正确地形成这种ActiveRecord的关联?

更新:

所以,我一直使用单表继承来解决问题试过了,我已经结束了一个用户表,记录只包含一个用户类型。虽然仍采用单表继承,如何让我的用户有多种类型的? (我知道这基本上是一个多一对多的关系,我只是不知道如何做到这一点使用STI。)

  ID | FIRST_NAME |姓氏| birth_date |城| zip_ code |电子邮件|键入| created_at |的updated_at
----+------------+-----------+------------+------+----------+-------+---------+----------------------------+----------------------------
  1 |晃|山冈| | | | |供应商| 2014年8月30日14:58:26.917333 | 2014年8月30日14:58:26.917333
  2 |金字塔|头| | | | |教师| 2014年8月30日15:02:04.70209 | 2014年8月30日15:02:04.70209
 

解决方案

单表继承可能是你所需要的。简而言之:它允许多个类与同类型的数据被放在一个表中。唯一的要求是,该表中有一个键入列,一个字符串

基本上,这是常识的。比方说,一个用户可以有通行证的事件:一个供应商的传球和教员的传球。他可能有两个。让我们创建一个模式,铭记我们需要不同类型的吧。但我们会用到它。现在,让我们只是坚持的has_many通过

 导轨G型通类型:字符串用户:引用事件:引用
 

迁移这一点,我们将不再需要修改我们的数据库。我们只修改红宝石。我们应该有一个类,我们需要以纪念其在协会的作用:

 班通<的ActiveRecord :: Base的
  belongs_to的:用户
  belongs_to的:事件
结束
 

所有权利。然后,我们有这样的用户事件

 类事件<的ActiveRecord :: Base的
  的has_many:通行证
  的has_many:用户通过:通行证
结束

类用户的LT;的ActiveRecord :: Base的
  的has_many:通行证
  的has_many:事件经过:通行证
结束
 

下面是其中STI魔术来。让我们创建两个类。

 导轨G型VendorPass --no迁移--parent =通行证
导轨G型FacultyPass --no迁移--parent =通行证
 

我们已经产生了一些类,而数据库表(我们不需要它们)。他们是空的,我们不会改变它:它们继承了一个,这就是我们所需要的。但是,我们需要创建我们的用户事件和新的通行证之间有一些额外的关联。最后,我发现这个工作:

 类事件<的ActiveRecord :: Base的
  #我们已经有这
  的has_many:通行证
  的has_many:用户通过:通行证

  # 新的东西!
  的has_many:vendor_passes
  的has_many:供应商,通过:vendor_passes,来源::用户

  的has_many:faculty_passes
  的has_many:faculty_members,通过:faculty_passes,来源::用户
结束

类用户的LT;的ActiveRecord :: Base的
  #我们已经有这
  的has_many:通行证
  的has_many:事件经过:通行证

  # 新的东西!
  的has_many:vendor_passes
  的has_many:vendor_events,通过:vendor_passes,来源:事件

  的has_many:faculty_passes
  的has_many:faculty_events,通过:faculty_passes,来源:事件
结束
 

Rails的维护自己的理解 VendorPass 这是这是一个,其键入 VendorPass ,同 FacultyPass

好的部分:

不难想象:数据结构,似乎是理智和逻辑 我们可以自由地在不改变数据库中添加更多类型的 ES

坏的部分:

没有办法添加额外的字段的只有特定的子类:他们都在同一个表 协会看上去有点重复,繁琐 在Rails的只允许键入是一个字符串,不是最快的类型比较

Basically, a User can participate in one or more events as either (or potentially both) a vendor and as a member of the event's faculty.

A user is, by default, categorized as neither a vendor nor a faculty member but rather attains either (or both) status(es) within the context of an event (e.g., the user has been admitted to an event as a member of its faculty).

It seems like what I want to say is that the user has many events through either (or both) the vendors or the faculty linking tables, but I'm not sure I'd go about representing this in my Rails model. Here's what I've tried so far:

class User < ActiveRecord::Base
  has_many :events through => vendors
  has_many :events through => faculty
end

Here's a sample of a query that I think I'd need to make:

Select * from vendors where user_id = 1;
Select * from faculty where user_id = 1;

Could someone provide some direction as to how to properly form this ActiveRecord association?

Update:

So, I've tried using single-table inheritance to solve the issue, and I've ended up with a user table which records containing only a single user type. While still using single-table inheritance, how do I get my users to have multiple types? (I know this is essentially a many-to-many relationship; I'm just not sure of how to accomplish this using STI.)

id | first_name | last_name | birth_date | city | zip_code | email |  type   |         created_at         |         updated_at
----+------------+-----------+------------+------+----------+-------+---------+----------------------------+----------------------------
  1 | Akira      | Yamaoka   |            |      |          |       | Vendor  | 2014-08-30 14:58:26.917333 | 2014-08-30 14:58:26.917333
  2 | Pyramid    | Head      |            |      |          |       | Faculty | 2014-08-30 15:02:04.70209  | 2014-08-30 15:02:04.70209

解决方案

Single-table inheritance might be what you need. In a nutshell: it allows several classes with the same kind of data to be put into one table. The only requirement is that that table has a type column, a string.

Basically, it's about common sense. Let's say, a user can have passes to an event: a vendor's pass and a faculty member's pass. He might have both. Let's create a Pass model, bearing in mind that we'll need different kinds of it. But we'll use it later. For now let's just stick to has_many through:

rails g model Pass type:string user:references event:references

Migrate this and we won't need to modify our database anymore. We'll only modify Ruby. We should have got a class Pass, we'll need to mark its role in association:

class Pass < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
end

All right. Then we'll have this sort of User and Event:

class Event < ActiveRecord::Base
  has_many :passes
  has_many :users, through: :passes
end

class User < ActiveRecord::Base
  has_many :passes
  has_many :events, through: :passes
end

Here's where the STI magic comes. Let's create two more classes.

rails g model VendorPass --no-migration --parent=Pass
rails g model FacultyPass --no-migration --parent=Pass

We've generated some classes without database tables (we don't need them). They are empty and we won't change it: they inherit a Pass and that's all we need. But we'll need to create some extra associations between our User, Event and the new passes. In the end, I've found this working:

class Event < ActiveRecord::Base
  # We already had this
  has_many :passes
  has_many :users, through: :passes

  # New stuff!
  has_many :vendor_passes
  has_many :vendors, through: :vendor_passes, source: :user

  has_many :faculty_passes
  has_many :faculty_members, through: :faculty_passes, source: :user
end

class User < ActiveRecord::Base
  # We already had this
  has_many :passes
  has_many :events, through: :passes

  # New stuff!
  has_many :vendor_passes
  has_many :vendor_events, through: :vendor_passes, source: :event

  has_many :faculty_passes
  has_many :faculty_events, through: :faculty_passes, source: :event
end

Rails maintains its own understanding of VendorPass which is "it's a Pass, whose type is VendorPass", same with FacultyPass.

The good parts:

Easy to imagine: data structure seems sane and logical We're free to add more types of Passes without changing the database

The bad parts:

No way to add extra fields to only specific subclasses of Pass: they're all in the same table Associations look a bit repetitive and cumbersome Rails only allows type to be a string, not the fastest type to compare