polymorphic-associations

Ruby on Rails: :include on a polymorphic association with submodels

ぃ、小莉子 提交于 2019-11-27 02:19:32
问题 When working with a polymorphic association, is it possible to run an include on submodels that are only present in some types? Example: class Container belongs_to :contents, :polymorphic => true end class Food has_one :container belongs_to :expiration end class Things has_one :container end In the view I'm going to want to do something like: <% c = Containers.all %> <% if c.class == Food %> <%= food.expiration %> <% end %> Therefore, I'd like to eager load the expirations when I load up c,

Rails polymorphic has_many :through

我的未来我决定 提交于 2019-11-27 01:37:40
问题 I'm pulling some data from an external API and would like to cache the results locally. I have a class SearchTerm , which I would like to be associated with a few different ActiveRecord types through the table searchable_items . I'm pretty sure I have the tables set up correctly, but something in my associations must be wrong. class Foo < ActiveRecord::Base has_many :search_terms, :as => :searchable, :through => :searchable_items end class Bar < ActiveRecord::Base has_many :search_terms, :as

ActiveRecord - querying polymorphic associations

老子叫甜甜 提交于 2019-11-26 22:38:24
问题 I am using polymorphic associations to track Comments in my project. All very straight forward stuff. The problem I have is in querying based on the polymorphic association and joining from the Comment model back to it's owner. So ... I have a Comment model class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end And a ForumTopics mode: class ForumTopic < ActiveRecord::Base has_many :comments, :as => :commentable end I have several other "commentable" models that

What is the best way to implement Polymorphic Association in SQL Server?

微笑、不失礼 提交于 2019-11-26 22:13:56
问题 I have tons of instances where I need to implement some sort of Polymorphic Association in my database. I always waste tons of time thinking through all the options all over again. Here are the 3 I can think of. I'm hoping there is a best practice for SQL Server. Here is the multiple column approach Here is the no foreign key approach And here is the base table approach 回答1: The two most common approaches are Table Per Class (i.e. a table for the base class and another table for each subclass

accepts_nested_attributes_for with belongs_to polymorphic

别说谁变了你拦得住时间么 提交于 2019-11-26 19:29:30
I would like set up a polymorphic relation with accepts_nested_attributes_for . Here is the code: class Contact <ActiveRecord::Base has_many :jobs, :as=>:client end class Job <ActiveRecord::Base belongs_to :client, :polymorphic=>:true accepts_nested_attributes_for :client end When I try to access Job.create(..., :client_attributes=>{...} gives me NameError: uninitialized constant Job::Client Dmitry Polushkin I've also had a problem with the "ArgumentError: Cannot build association model_name. Are you trying to build a polymorphic one-to-one association?" And I found a better solution for this

Why polymorphic association doesn't work for STI if type column of the polymorphic association doesn't point to the base model of STI?

删除回忆录丶 提交于 2019-11-26 19:08:54
问题 I have a case of polymorphic association and STI here. # app/models/car.rb class Car < ActiveRecord::Base belongs_to :borrowable, :polymorphic => true end # app/models/staff.rb class Staff < ActiveRecord::Base has_one :car, :as => :borrowable, :dependent => :destroy end # app/models/guard.rb class Guard < Staff end In order for the polymorphic assocation to work, according to the API documentation on Polymorphic Assocation, http://api.rubyonrails.org/classes/ActiveRecord/Associations

Something like inheritance in database design

人盡茶涼 提交于 2019-11-26 18:58:33
问题 Suppose you were setting up a database to store crash test data of various vehicles. You want to store data of crash tests for speedboats, cars, and go-karts. You could create three separate tables: SpeedboatTests, CarTests, and GokartTests. But a lot of your columns are going to be the same in each table (for example, the employee id of the person who performed the test, the direction of the collision (front, side, rear), etc.). However, plenty of columns will be different, so you don't want

Foreign key to one of many tables?

白昼怎懂夜的黑 提交于 2019-11-26 16:59:04
问题 The usual way of setting a foreign key constraint is to choose which table the foreign key will point to. I'm having a polymorphic relation between 1 table and a set of table. That means that this table will have a relation with one of those tables in the set. eg. images: person_id, person_type subordinates: id, col1, col2...col9 products: id, colA, colB...colZ In the above example, if person_type is "subordinates" then person_id should be a foreign key to subordinates.id and the same goes

How to implement polymorphic associations in an existing database

旧街凉风 提交于 2019-11-26 16:41:57
Polymorphic assiociations (PA's) is quite a mouthful for a relatively simple database requirement: let various tables have child records in one shared table. The classic example is a single table with comment records that apply to different not necessarily kindred entities. In this question Mark did an excellent job showing three common approaches to implement PA's. I want to use the base table approach, which is described in more detail in an equally excellent answer by Bill Karwin . A concrete example would look like this: The primary keys of the entities refer to identical key values in the

ActiveRecord, has_many :through, and Polymorphic Associations

两盒软妹~` 提交于 2019-11-26 14:51:27
Folks, Want to make sure I understand this correctly. And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. That said, consider the following... class Widget < ActiveRecord::Base has_many :widget_groupings has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'" has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_type = 'Alien'" end class Person < ActiveRecord::Base has_many