polymorphic-associations

RoR: has_one "or the other"? (Or, polymorphism without the inheritance.)

Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure...

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

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 FK to either countries ...

ActiveRecord - querying polymorphic associations

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 joinging from the Comment model back to it's owner. So ... I have a Comment model class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic ...

Polymorphic Associations in Rails

How do polymorphic associations work in Rails? What are their advantages? Is there a way to add belongs_to method just by running a migration? ...

Referencing foreign keys in the same column

I'm designing a bd-scheme for mysql. My db stores 3 kinds of points: a, b or c and a path is composed by n pair of points: Route = [ (a1 or b1 or c1 ; a2 or b2 or c2), (a2 or b2 or c2 ; a3 or b3 or c3), ...] create table a_points ( point_id serial not null, owner_id bigint unsigned not null, name varchar(20) not null, ...

polymorphic associations and routing

Hi I am very new to rails and am having a problem with routing when using polymorphic associations. On I have a link "Add Comment" on the page installations/1/onposts/2 the link then goes to onposts/2/comments, this works and you can add a comment on this page, when you submit it just redirects to stay on the same page. I want to have a...

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

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 :commen...

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

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 ...

Can I narrow down the type of classes retrieved by a Polymorphic relationship in Rails?

I have a polymorphic relationship in Rails, but in one particular instance of use, I'd only like to retrieve records for a specific class. What's the best approach to do this? ...

Polymorphic habtm relationships with Rails/ActiveRecord

How would I go about creating a polymorphic has_and_belongs_to_many relationship with Rails/ActiveRecord? Most of the examples I see involve creating a belongs_to relationship which limits my polymorphic-side to being related to only one parent: Table: Task Table: Tasks_Targets Table: CustomerStore Table: SoftwareSystem Both Custom...

Rails: Many to many polymorphic relationships...

See comments for updates. I've been struggling to get a clear and straight-forward answer on this one, I'm hoping this time I'll get it! :D I definitely have a lot to learn still with Rails, however I do understand the problem I'm facing and would really appreciate additional help. I have a model called "Task". I have an abstract mode...

Polymorphic Association with multiple associations on the same model

I'm slightly confused about a polymorphic association I've got. I need an Article model to have a header image, and many images, but I want to have a single Image model. To make matters even more confusing, the Image model is polymorphic (to allow other resources to have many images). I'm using this association in my Article model: cla...

Can I accomplish this with multiple concrete supertables?

I have two tables, "contents" and "profiles" that can be commented on and rated. I have looked at using polymorphic associations and have decided against it. If I were to use polymorphic associations, the table "ratings" and "comments" would both have this feature. Is this possible with a concrete supertable implementation? And if so...

Polymorphic Rails Associations (going backwards?)

Hi, I have a many to one polymorphic rails association in my rails app for a 'case' model. There are many things that have cases, so I access the case each thing has by doing 'thing_that_has_a_case.case'. However, I'm trying to go the other way and I'm not sure how. I have access to the case object but I want to access the thing tha...

Is there a name to this Ruby on Rails common model pattern? Polylink?

There seems to be no name for this common model pattern. It's used in many plugins like acts_as_taggable[_whatever] and it basically allows linking a certain model, like Tag, with any other model without having to put ever-more belongs_to statements in the Tag model. It works by having your model (Tag) linked to a polymorphic join mo...

Best way to model a user's "friendship" with different entities?

I have the following entities: User Company Organization Users need to be able to add Users, Companies, Organizations, and future party objects to their friend list. My original idea involved using a Friendship object with a polymorphic relationship to the friend like so: Simplified Friendship schema: user_id friendable_id friend...

How can I optimize this MySQL query that involves two left joins?

I cannot figure out why my query slows down. What it boils down to are four tables: team, player, equipment, and metadata. Records in player and equipment have a FK to team, making team a parent of player and equipment. And all three of those tables' rows each have a record in metadata which stores things like creation date, creator ...

Rails Polymorphic relationship and link_to

Here's my Schema class Menu < ActiveRecord::Base belongs_to :menuable, :polymorphic => true end class Page < ActiveRecord::Base has_one :menu, :as => :menuable end class Links < ActiveRecord::Base has_one :menu, :as => :menuable end I want to link to a polymorphic class in the Menu view using link_to, e.g. <%= link_to menu.na...

MySQL Multiple select

I have an indextable which hold a content_id and type colomn. (eg. content_id=1, type=audio. content_id=1, type=video) Would it be possible to get the right data based upon content_id from the right tables (tables: contentvideo, contentaudio)? ...

Creating Ruby ActiveRecord objects with associations

I have Persons and Teams that have Addresses (polymorphic association) I need to create persons and addresses separately in code: person = Person.new address = Address.new and then link them together using something like: person.addressable.push(address) finally, do: person.save! This however gives a You have a nil obje...