如何组织此表,以便可以根据访问它的对象的类型进行访问

编程入门 行业动态 更新时间:2024-10-23 05:34:05
本文介绍了如何组织此表,以便可以根据访问它的对象的类型进行访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有该表包含一个名为icon_for的字段,我已经使用该字段来检查图标是否可以用于用户或问候。

I have this table which contains a field called 'icon_for' which I have been using to check if the icon can be used for a 'User' or a 'Greeting'.

这显然不是最好的方式,就像我想使用一个图标来说问候和用户,甚至是其他一些对象我会被卡住。

This is obviously not the best way to do this as if I want to use an icon for say both a 'Greeting' and a 'User' or even some other object I would be stuck.

我需要再创建一个名为icons_object的表,然后列出icon_id和object_name吗? 好​​像我会做很多重复的icon_id,如果一个图标用于几个对象,但这是可以去吗?

Do I need to make another table called icons_object and then list the icon_id and object_name? Seems like I would have to do a lot of repeating of the icon_id's if an icon is used for several objects but is this the way to go?

另外Rails中的这个表关联将是什么。说表被称为icons_objects。 然后在Rails中它将是

Also what would this table association in Rails be. Say the Table is called icons_objects. Then in Rails would it be

Icon.rb

图标has_many:icon_objects ???

icon has_many :icon_objects ?????

在这里完全混淆,所以对NOOB的任何帮助将不胜感激。

Totally confused here so any help for a NOOB would be appreciated.

有没有办法做这个规范化?

Is there a way to do this normalized?

推荐答案

你想使用多态关联。例如:

You want to use polymorphic associations. For example:

class Icon < ActiveRecord::Base belongs_to :iconable, :polymorphic => true end class Greeting < ActiveRecord::Base has_many :icons, :as => :iconable end class User < ActiveRecord::Base has_many :icons, :as => :iconable end

然后,您可以从问候语或用户获取图标:

Then you could get to icons from greetings or user:

@user.icons @greeting.icons @icon.iconable #returns the parent type.

或者如果您想查询特定类型的图标:

Or if you wanted to query icons for a particular type:

Icon.where(:iconable_type => "greeting")

这种方法与你已经采用的方法不同。执行此方法的好处是,您可以获取父对象,而不用担心它是什么类型,就像您尝试在此线程中实现其他答案一样。

This approach is not unlike the one you have already taken. The benefits of doing this method is that you can get the parent object without worrying about what type it is, like you would if you tried to implement the other answer in this thread.

你只需要运行一些迁移来使一切正常工作。您可以在 guides.rubyonrails/association_basics.html#polymorphic查看更多信息-associations 。

You will just need to run some migrations to get everything working properly. You can see more information at guides.rubyonrails/association_basics.html#polymorphic-associations.

编辑每个评论:

严格来说,你是对的。您不能具有指向用户和问候语的多态记录。但是,您可以进行一个小的更改,并创建另一个模型,这将像联接表一样。

Strictly speaking, you're correct. You cannot have a polymorphic record which points to a User and a Greeting. However, you can make a small change and create another model, which would kind of act like a join table.

class IconConnection < ActiveRecord::Base belongs_to :inconable, :polymorphic => true belongs_to :icon end class Icon < ActiveRecord::Base has_many :icon_connections end class Greeting < ActiveRecord::Base has_many :icon_connections, :as => :iconable has_many :icons, :through => :icon_connections end class User < ActiveRecord::Base has_many :icon_connections, :as => :iconable has_many :icons, :through => :icon_connections end

每个IconConnection将指向一个问候语或一个用户,每个的IconConnections将指向同一个图标。

Each IconConnection would point to either a Greeting or a User, and each of the IconConnections would point to the same Icon.

此设置假设您可以为每个用户和问候语多个图标,但如果没有,您可以替换 has_many 的 has_one 的

This setup assumes that you can have multiple icons for each user and greeting, but if not, you can replace the has_many's with has_one's

更多推荐

如何组织此表,以便可以根据访问它的对象的类型进行访问

本文发布于:2023-06-07 21:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以根据   对象   类型   组织

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!