polymorphic-associations

Suggestions on how to track tag count for a particular object

Hi, I'm looking for suggestions on how to track the number of tags associated with a particular object in Rails. I'm using acts_as_taggable_on and it's working fine. What I would like to be able to do is search for all objects that have no tags, preferably through a scope i.e. Object.untagged.all My first thought was to use an after_sa...

Two Different Types of Associatons on the Same Two Tables in Rails

I have two models, users and themes, that I'm currently joining in a HABTM association in a themes_users table. Basically, after a user creates a new theme, it becomes available to other users to select for their own use. However, only the original creator of the theme should have the ability to edit it. So, I need to have some other ki...

Using Ruby on Rails, can a polymorphic database be created in a few steps (with polymorphic associations)?

I thought it could be created in a few steps but it can't yet: rails poly cd poly ruby script/generate scaffold animal name:string obj_type:string obj_id:integer rake:migrate ruby script/generate scaffold human name:string passportNumber:string rake:migrate ruby script/generate scaffold dog name:string registrationNumber:string rake:...

how to avoid polymorphic associations

Given you have to implement a news feed like the one seen in social networks, ex facebook. Currently I'm using a News class which has a polymorphic assocation which can be of any kind like Image, Comment, Friendship, GroupMembership, etc. Whenever an Object is created, as News is created too. It's working fine with AR but I get into tro...

Generic Database table design

Just trying to figure out the best way to design my table for the following scenario: I have several areas in my system (documents, projects, groups and clients) and each of these can have comments logged against them. My question is should I have one table like this: CommentID DocumentID ProjectID GroupID ClientID etc Where only on...

Find parents without or with children in polymorphic associations in rails

I have two models: cars and pictures in my RoR project class Car < ActiveRecord::Base has_many :pictures, :as => :imageable, :dependent => :destroy end and class Picture < ActiveRecord::Base belongs_to :imageable, :polymorphic => true, :dependent => :destroy end How I can find all cars only with child pictures? ...

MySQL: Two n:1 relations, but not both at once

Sorry for the title, it's difficult to explain. I need a data model similar to this: As you can see, a set can belong to both a user or a school. My problem: It should only be allowed to belong either to a user OR a school. But never both at the same time. How can I solve this problem? ...

Using polymorphic paths with nested associations.

I have a polymorphic association that looks like this: class Line < ActiveRecord::Base belongs_to :item, :polymorphic => true end class Education < ActiveRecord::base has_many :lines, :as => :item end class Work < ActiveRecord::base has_mayn :lines, :as => :item end I'd like a simple way to create a new Line from the parent...

Rails Polymorphic Model. How to show form validation erros on views?

I have everything on my polymorphic associations working, but if I have model validations, the erros don't show up. In my controller I have: def create @locatable = find_locatable @geographic_location = @locatable.geographic_locations.build(params[:geographic_location]) if @geographic_location.save flash[:notice] =...

Rails Polymorphic has_many

Using Ruby on Rails, how can I achieve a polymorphic has_many relationship where the owner is always of a known but the items in the association will be of some polymorphic (but homogenous) type, specified by a column in the owner? For example, suppose the Producer class has_many products but producer instances might actually have many ...

How to apply different validation rule according to polymorphic association type (Rails)?

I have Rails polymorphic model and I want to apply different validations according to associated class. ...

RoR: Should I use belongs_to, :polymorphic in this scenario?

I am working on a project where many ActiveRecord models can have a conversation associated with it. Users can discuss just about every aspect of the site. I have two ideas as to how this should be implemented. 1) Use a belongs_to in the asset, not the conversation - conversation will be totally unaware of its asset class Product< Acti...

has_many_polymorphs & Forms

The way this program needs to work is like this: A person can view an event and then choose to either register directly for an event or for a team (which is registered for an event). class Event has_many_polymorphs :registrants, :from => [:users, :teams], :through => :registrations end class Team belongs_to :registrant, :polymorphi...

Foreign key to one of many tables?

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

How do I specify associations in Rails that pass through several models

I'm writing some tricky polymorphic relationships to handle tagging. I have a Tag model, and a Tagging model which belongs_to a polymorphic taggable. I have an Item model, which has_many :taggings, :as => :taggable, and has_many :tags, :through => :taggings, so that I can call @item.tags. This is all working ok. I want to bring anoth...

Tricky Association Question Rails

#Vote Model belongs_to :voteable, :polymorphic => true belongs_to :voter, :polymorphic => true #votes table Voteid: integer, vote: boolean, voteable_id: integer, voteable_type: string, voter_id: integer, voter_type: string, created_at: datetime, updated_at: datetime #User has_many :voteables, :foreign_key => :voter_id, :class_name =...

Writing proper validation errors ?

Update : Gutted entire question with more thorough description Ok same question with different names. In my model, I do validate the presence of. class QuickFact < ActiveRecord::Base belongs_to :organization validates_presence_of :quick_fact, :content But if either is blank, it errors out with : Missing template organizations/...

Bi-directional polymorphic join model in Rails?

I'm working on a multi-site CMS that has a notion of cross-publication among sites. Several types of content (Articles, Events, Bios, etc) can be associated with many Sites and Sites can have many pieces of content. The many-to-many association between content pieces and sites must also support a couple common attributes for each conte...

RoR Achievement System - Polymorphic Association & Design Issues

I'm attempting to design an achievement system in Ruby on Rails and have run into a snag with my design/code. Attempting to use polymorphic associations: class Achievement < ActiveRecord::Base belongs_to :achievable, :polymorphic => true end class WeightAchievement < ActiveRecord::Base has_one :achievement, :as => :achievable end ...

Foreign key constraint that points to one of several tables

I have a table with one column source_id whose value should be the primary key of another table, though which table it is will vary from record to record. Every record must have a value for source_table that specifies the table for the source record, and a value for source_id that specifies the row in the source table. Is there any way ...