polymorphic-associations

Rails - Succinctly finding the parent resource of a polymorphically nested resource

Suppose I have a polymorphic structure like this. map.resources :bar, :has_many => :foo map.resources :baz, :has_many => :foo map.resources :qux, :has_many => :foo class Foo belongs_to :parent, :polymorphic => true end class FooController < AC before_filter :find_parent ... private def find_parent # ugly way: @parent = if ...

RESTfully destroy polymorphic association in Rails?

How do I destroy the association itself and leave the objects being associated alone, while keeping this RESTful? Specifically, I have these models: class Event < ActiveRecord::Base has_many :model_surveys, :as => :surveyable, :dependent => :destroy, :include => :survey has_many :surveys, :through => :model_surveys end class Model...

validation form with polymorphic association

Hi i have 2 models class User < ActiveRecord::Base has_one :core, :as => :resource validates_presence_of :name end class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true validates_presence_of :email end For insert a new user i use a form like this: <%= error_messages_for :user %> <% form_for :user, :...

Inheritance and polymorphic-associations in rails

I have a User model, which belongs to Profile (belongs_to polymorphic). One model comes in two subclasses, but the *profile_type* in User always correspond to the parent model. User < ActiveRecord::Base belongs_to :profile, :polymorphic => true SomeProf < ActiveRecord::Base has_one :user, :as => :profile SomeDeepProf1 < SomeProf ...

Using polymorphic models in nested forms

For an answer model I want to save different types of answer (e.g.decimal, string, text, ...) in different tables. Polymorphic seems the way to go, but how can I receive the users answers from a nested form using fields_for? I now in advance, what type is asked for, but how do I tell in the controller in which table the answer should be...

Polymorphic has_many through Controllers: Antipattern?

I'm tempted to say yes. A contrived example, using has_many :through and polymorphs: class Person < ActiveRecord::Base has_many :clubs, :through => :memberships has_many :gyms, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :member, :polymorphic => true end class Club < ActiveRecord::Base has_ma...

paperclip polymorphic association and saving

I changed my photo.rb model to be polymorphic and be usable to all sorts of other models needing to save images and it works fine except I can't figure out how to save new attachments properly through the parent model. Any ideas? Do I have to approach this differently somehow? As, its also not getting the imageable_type...which i'll h...

Finding all by Polymorphic Type in Rails?

Is there a way to find all Polymorphic models of a specific polymorphic type in Rails? So if I have Group, Event, and Project all with a declaration like: has_many :assignments, :as => :assignable Can I do something like: Assignable.all ...or BuiltInRailsPolymorphicHelper.all("assignable") That would be nice. Edit: ... such that...

SQL Conditional / Case Joining / Polymorphic Associations?

Hi, I'm trying to implement something similar to Ruby on Rails' polymorphic relationships. I have the following three tables : Events Users Organisations An event can be owner by either a user or an organisation, so my Events table includes the columns: owner_type and owner_id. I can easily list all events that belong to either users...

Rails Photos, Users, Comments

Hi, It's driving me crazy. I have 3 models. User, Photo, Comments. Here is what I want to do. A user has many photos and comments A photo belongs to a user and has many comments And a comment belongs to user and to a photo So my models have these associations: User has_many :photos, :dependent => :destroy has_many :comments, :de...

rails, set polymorphic association

How should the keyword_list=(value) method look ? class Keyword < ActiveRecord::Base belongs_to :keywordable, :polymorphic => true end class Article < ActiveRecord::Base has_many :keywords, :as => :keywordable #getter def keyword_list keywords.map {|keyword| keyword.name + ","}.to_s.chop end #setter def keyword_...

Caching a column in a polymorphic relationship

I have content management system application that uses a polymorphic tree table as the core of its arrangement. I've come into a problem where once the tree grows quite large, and because we have quite a few different modules (about 25), just doing :include => :instance doesn't cut the mustard. Instance is the name of our polymorphic rel...

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

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

Rails Model for Playlist that can contain tracks and albums using polymorphism

I struggle to find a model how to store a playlist with different type of items on it in Rails. Consider I have class Track end class Album has_many :tracks end class PlaylistItem belongs_to :playable belongs_to :playlist end class Playable belongs_to :playable, :polymorph => true end class Playlist has_many :playlist_i...

Rails Polymorphic Association with multiple associations on the same model

My question is essentially the same as this one: http://stackoverflow.com/questions/1168047/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. H...

Rails AR validates_uniqueness_of against polymorphic relationship

Is it posible to validate the uniqueness of a child model's attribute scoped against a polymorphic relationship? For example I have a model called field that belongs to fieldable: class Field < ActiveRecord::Base belongs_to :fieldable, :polymorphic => :true validates_uniqueness_of :name, :scope => :fieldable_id end I have several...

Polymorphic Associations in Doctrine?

Is there an equivalent to Rails-like polymorphic associations in Doctrine? I'm trying to do something similar to: http://railscasts.com/episodes/154-polymorphic-association ...

Ruby on rails with differnt user types

Hi everyone, Im trying to build a application that has different kinds of users, Im 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 ...

STI and polymorphs

Hi, I have problem with my code class Post < ActiveRecord::Base end class NewsArticle < Post has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at' end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true, :counter_cache => true end And on attempt go get comments for s...

Problem with eager load polymorphic associations using Linq and NHibernate

Is it possible to eagerly load polymorphic association using Linq and NH? For example: Company is base class, Department is inherited from Company, and Company has association Employees to the User (one-to-many) and also association to the Country (many-to-one). Here is mapping part related to inherited class (without User and Country c...