polymorphic-associations

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

不打扰是莪最后的温柔 提交于 2019-11-26 11:07:34
问题 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

Why can you not have a foreign key in a polymorphic association?

核能气质少年 提交于 2019-11-26 09:44:24
Why can you not have a foreign key in a polymorphic association, such as the one represented below as a Rails model? class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #... end class Event < ActiveRecord::Base has_many :comments, :as => :commentable end A foreign key must reference only one parent table. This is fundamental to both SQL syntax, and relational theory. A Polymorphic Association is when a given

Rails Polymorphic Association with multiple associations on the same model

坚强是说给别人听的谎言 提交于 2019-11-26 07:59:34
问题 My question is essentially the same as this one: Polymorphic Association with multiple associations on the same model However, the proposed/accepted solution does not work, as illustrated by a commenter later. I have a Photo class that is used all over my app. A post can have a single photo. However, I want to re-use the polymorphic relationship to add a secondary photo. Before: class Photo belongs_to :attachable, :polymorphic => true end class Post has_one :photo, :as => :attachable,

accepts_nested_attributes_for with belongs_to polymorphic

狂风中的少年 提交于 2019-11-26 06:57:45
问题 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 回答1: I've also had a problem with the "ArgumentError: Cannot build association model_name. Are you

MySQL - Conditional Foreign Key Constraints

自闭症网瘾萝莉.ら 提交于 2019-11-26 06:55:23
I have following 'comments' table in my app: comments -------- id INT foreign_id INT model TEXT comment_text TEXT ... the idea of this table is to store comments for various parts of my app - it can store comments for blog post i.e: 1|34|blogpost|lorem ipsum... user picture: 2|12|picture|lorem ipsum... and so on. now, is there a way to force FOREIGN KEY constraint on such data? i.e. something like this in comments table: FOREIGN KEY (`foreign_id`) REFERENCES blogposts (`id`) //but only when model='blogpost' You're attempting to do a design that is called Polymorphic Associations . That is, the

How to implement polymorphic associations in an existing database

我们两清 提交于 2019-11-26 04:54:12
问题 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

Why can you not have a foreign key in a polymorphic association?

坚强是说给别人听的谎言 提交于 2019-11-26 02:04:35
问题 Why can you not have a foreign key in a polymorphic association, such as the one represented below as a Rails model? class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #... end class Event < ActiveRecord::Base has_many :comments, :as => :commentable end 回答1: A foreign key must reference only one parent table.

Possible to do a MySQL foreign key to one of two possible tables?

孤者浪人 提交于 2019-11-25 22:46:59
问题 Well here\'s my problem I have three tables; regions, countries, states. Countries can be inside of regions, states can be inside of regions. Regions are the top of the food chain. Now I\'m adding a popular_areas table with two columns; region_id and popular_place_id. Is it possible to make popular_place_id be a foreign key to either countries OR states. I\'m probably going to have to add a popular_place_type column to determine whether the id is describing a country or state either way. 回答1: