polymorphic-associations

many-to-many polymorphic

I hope this is a simple problem for someone because I am going round and round in circles I have an Author who can author/compose or co-author, either Songs and or Tunes. A Song constitutes the lyrics, title etc. The Tune is the score and mp3 url. I have tried the following without success class Author < ActiveRecord::Base has_many ...

Ruby: Declarative_authorization polymorphic associations

I have two models (Item and Theme). They are both owned by a third model Users with a has_many association (User has many Themes and Items). Both Item and Theme have_many :images. The Image model is a polymorphic association so the table has the columns imageable_id and imageable_type. If I had both an Item with ID 1 and a Theme with ...

Is a polymorphic association appropriate here?

Here's what I'm thinking: class Widget < ActiveRecord::Base has_one :widget_layout end class WidgetLayout < ActiveRecord::Base belongs_to :widget belongs_to :layoutable, :polymorphic => true end class InformationalLayout < WidgetLayout has_one :widget_layout, :as => :layoutable end class OneWayCommunicationLayout < WidgetLayo...

Rails, Am I wrong to think Polymorphic Associations are overrated, limiting, and unnecessary?

OK, so I've been toying with different ways of organizing my Rails 3 apps regarding STI and Polymorphic Associations. I was trying to find a way that is both easy to code, and easy to use, and has the highest chance of being the most compatible with any future changes. After alot of reading a lot of optionated (and often lacking and po...

Join Model + STI + Polymorphic Associations - Can ActiveRecord not do this?

The question is at the bottom, but the code describes the bulk of it :) Here's the base of the code, shortened to get to the core of it: ActiveRecord::Base.class_eval do def self.joins(*others) has_many :parent_join_models, :as => :child has_many :child_join_models, :as => :parent options = others.extract_options! th...

Rails: Query to get recent items based on the timestamp of a polymorphic association

I have the usual polymorphic associations for comments: class Book < ActiveRecord::Base has_many :comments, :as => :commentable end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end I'd like to be able to define ...

Creating forms for polymorphic associations in Rails

I have a couple classes that can each have comments: class Movie < ActiveRecord::Base has_many :comments, :as => :commentable end class Actor < ActiveRecord::Base has_many :comments, :as => :commentable end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end How do I create a form for a n...

Eager Load Polymorphic has_many :through Associations in ActiveRecord?

How do you eager load polymorphic has_many :through associations in Rails/ActiveRecord? Here's the base setup: class Post < ActiveRecord::Base has_many :categorizations, :as => :categorizable has_many :categories, :through => :categorizations end class Category < ActiveRecord::Base has_many :categorizations, :as => :category h...

Polymorphic Association with Comments - Ruby on Rails

I am having a bit of trouble getting these polymorphic associations to work completely. I followed this tutorial www.railscasts.com/episodes/154-polymorphic-association, but that seems to only work if I am path /controller/ID#/comments when making a new post. If I try to render a partial comment form right on /controller/ID# I get this e...

Rails 3 - polymorphic_path - How to Create One given a table

Hello, i have table AuditLog with fields including: audited_id | audited_type That results in data like: 108 | Photo 303 | Comment What I want to do is create a link to the item, so for the example above: here is the photo I'm trying to use a polymorphic_path but am getting an error: "undefined method `model_name' for Fixnum:Class" ...

How to add methods to a rails polymorphic association?

So, if I have a polymorphic association throughout my application, is there a way to add methods to it? For instance, if :post, :photo and :user are all associated to :rankings with a polymorphic association named :rankable, can I somehow create methods on :rankable that would then be inherited by :post, :photo or :user? My guess is th...

How can I sort by the average of a polymorphic child's attribute with ActiveRecord 2.3?

I have this schema: class Comment has_many :ratings, :as => :target end class Rating belongs_to :target, :polymorphic => true end I want to write a named scope that will sort comments by their average rating, without fetching the whole list of comments and then fetching all their ratings. I think I need to use :include and :grou...

How to join across polymorphic tables in one query?

Hi all, I have 2 polymorphic associations through which I need to query. I have a news_article table which has a polymorphic association to teams, players, etc. Those teams, players, etc have a polymorphic association to photos through phototenic. I need to find all articles that have at least one picture that is 500px wide. The Ar...

has many polymorphic images through another polymorphic model

Greets to ruby developers. I'm stuck with ActiveRecord model associations. I illustrate what I want to do by a tables. I have these tables: Products: +----+-----------+ | id | name | +----+-----------+ | 1 | Cellphone | +----+-----------+ Images: +----+-------+--------------+----------------+ | id | url | imageable_id | image...

Adding tags to polymorphic models in rails

I'm adding tags to several models (Posts, Articles, Photos, etc). I'm aware of rails tagging plugins but prefer not to use them, as they don't quite meet my specific needs. I know the typical way of implementing polymorphic tagging support is to use 2 tables Tags, Taggings and setup the appropriate has_many :through relationships. B...

How do I stop Rails from misidentifying this class in a polymorphic :belongs_to?

I'm moving some code from being pulled in via SVN's externals function to be part of the code base proper. It's all Rails plugins and doing this seems to have broken part of my code. It seems like Rails is misinterpreting a :belongs_to statement. I have belongs_to :target :polymorphic => true in one of my classes and previously that has...

Sharing model for polymorphic associations

Assume a polymorphic association, say 'business' and 'staff', both of which are 'hourable' (meaning they have hours assigned to them). What's the recommended approach to have the 'hour' model performs the same methods on the hours of either a business object or a staff object? For a simple example, the 'hour' model might contain: def...

Problem with routing rails 3.0.0 and --force-plural

Hi, sorry for bad English. For example i execute script/rails generate scaffold goods title:string --force-plural Then i follow to http://localhost:3000/goods/new and got error undefined method `goods_index_path' for #<#:0x7f1e7cb32530> I found some line involved in this behavour in polymorphic_routes.rb, but can't understand what ...

Defining an object's behavior using polymorphic inheritance

Hi all! Today I was faced with a challenge to create different behaviors for my shopping cart model. That's because the owner of the online shopping wanted to create some promotions like buy 1, get 5 or get 25% discount + some extra stuff, etc... I thought of doing it with polymorphic inheritance, where my Cart model will only hold the...

accepts_nested_attributes_for with belongs_to polymorphic

Hi, 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_at...