polymorphic-associations

Rails: includes with polymorphic association

做~自己de王妃 提交于 2019-11-28 20:56:32
问题 I read this interesting article about Using Polymorphism to Make a Better Activity Feed in Rails. We end up with something like class Activity < ActiveRecord::Base belongs_to :subject, polymorphic: true end Now, if two of those subjects are for example: class Event < ActiveRecord::Base has_many :guests after_create :create_activities has_one :activity, as: :subject, dependent: :destroy end class Image < ActiveRecord::Base has_many :tags after_create :create_activities has_one :activity, as:

Laravel - Eager Loading Polymorphic Relation's Related Models

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 16:24:11
I can eager load polymorphic relations/models without any n+1 issues. However, if I try to access a model related to the polymorphic model, the n+1 problem appears and I can't seem to find a fix. Here is the exact setup to see it locally: 1) DB table name/data history companies products services 2) Models // History class History extends Eloquent { protected $table = 'history'; public function historable(){ return $this->morphTo(); } } // Company class Company extends Eloquent { protected $table = 'companies'; // each company has many products public function products() { return $this->hasMany

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

喜欢而已 提交于 2019-11-28 08:34:49
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, because I know I will need every last one of them. Is there any way to do so? Just defining a regular

Ruby on rails with different user types

懵懂的女人 提交于 2019-11-28 06:34:11
问题 I'm trying to build a application that has different kinds of users, I'm using authlogic for user authentication. So I have one user model that has the required field for authlogic to do its magic. I now want to add a couple of different models that would describe the extra fields for the different kinds of users. Lets say that a user registers, he would then select his user type, when he is done registering he would be able to add information that is specific for his user model. What would

Polymorphic Assocations using Integer ID type fields

若如初见. 提交于 2019-11-28 03:15:26
问题 I have a table Foo that has a polymorphic belongs_to association called bar . The foos table has the standard bar_id column. However, instead of a string-based bar_type column, I have an integer bar_type_id column. This column references the id column in the table bar_types . bar_types.name holds the name of the class that represents the class of the particular bar instance. Does Rails (ideally >=2.3.10) allow for this type of polymorphic association? 回答1: We did it by overriding the

ActiveRecord - querying polymorphic associations

大兔子大兔子 提交于 2019-11-27 18:08:39
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 aren't important right now. All of this works. What I am trying to do is find all of the Comments that

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-27 17:42:22
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/ClassMethods.html#label-Polymorphic+Associations that I have to set borrowable_type to the base_class of STI

Something like inheritance in database design

我们两清 提交于 2019-11-27 17:18:38
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 to just put all of the test data in a single table because you'll have quite a few columns that will

Foreign key to one of many tables?

折月煮酒 提交于 2019-11-27 15:01:10
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 with products. So I wonder, is it possible to have a foreign key to one of many tables, or do you have to

In a StackOverflow clone, what relationship should a Comments table have to Questions and Answers?

Deadly 提交于 2019-11-27 04:17:01
In an application similar to StackOverflow that I am building, I am trying to decide what relationship my Questions , Answers and Comments tables should have. I could have Questions and Answers both be represented by a single table Posts . That would allow Comments to have a single foreign key to Posts . But if Questions and Answers are separate tables, what relationships should Comments have to each of these? UPDATE: Although the chosen answer recommends a Class Table Inheritance approach and this seems like the best approach in database terms, this option is not supported by the Rails ORM.