English 中文(简体)
如何组织这一表格,以便能够根据从哪类物体获取该表格。
原标题:How to organize this table so it can be accessible depending on the type of object accessing it

“entergraph

我的表格中有一个称为“icon”的栏目,我一直用来核对“icon”是否可用于用户或用户。

这显然不是这样做的最佳途径,因为如果我想用一线来说绿化和用户,甚至其他物体,我就会被 st。

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?

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

Icon.rb

icon有吗?

这里完全混淆不清,因此,希望得到任何帮助。

是否有办法实现这一正常化?

最佳回答

你希望利用多形态协会。 例如:

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

之后,您可以从问候或用户那里获得启示:

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

或者,如果您想询问某类特定类型:

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

这种做法与你已经采取的做法不同。 采用这种方法的好处是,你能够拿到母子,而不会担心什么类型,正如你试图在座右边做其他回答一样。

你们只是需要管理一些移民,以便让一切顺利发挥作用。 可在查阅更多信息。

Edit per comment:

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

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

这一设置假设,您可为每个用户和问候人提供多条标语,但如果没有,你可以将<代码>has_manys改为has_one。 页: 1

问题回答

It sounds like you are talking about a many-to-many relationship. As in icons can belong to more than one object and an object can have more than one icon. You need a matching table for that kind of relationship... something like icon_objects that you talked about. Then, your models you will use either a has_many :through relationship or has_and_belongs_to_many. If icon_objects only exists to map objects to icons then you probably want to use has_and_belongs_to_many. If you think you ll need to store additional information about a specific icons relationship to a specific object, then you might need an actual IconObject model and should use has_many :through to map icons to objects.

If an object can only have one icon then put the foreign key in each of the object tables (so users has an icon_id field, and so does the greetings table). Then User and Greeting will have a belongs_to relationship with Icon, and Icon will have a has_many relationship with both User and Greeting.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签