polymorphic-associations

Finding all by Polymorphic Type in Rails?

情到浓时终转凉″ 提交于 2019-12-03 07:22:20
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 Assignable.all returns [Event, Group, Product] (array of classes) There is no direct method for this. I wrote this monkey patch for ActiveRecord::Base . This will work for any class. class ActiveRecord::Base def self.all_polymorphic_types(name) @poly_hash ||= {}.tap do

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

一个人想着一个人 提交于 2019-12-03 06:58:28
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 out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either Bar or Baz assigned. # 4. Bar and Baz have only ONE common property, and aren't # related in either

MYSQL join tables based on column data and table name

我们两清 提交于 2019-12-03 06:17:46
I'm wondering if this its even posible. I want to join 2 tables based on the data of table 1. Example table 1 has column food with its data beeing "hotdog". And I have a table called hotdog. IS it possible to do a JOIN like. SELECT * FROM table1 t join t.food on id = foodid I know it doesnt work but, its even posible, is there a work arround?. Thanks in advance. Bill Karwin No, you can't join to a different table per row in table1 , not even with dynamic SQL as @Cade Roux suggests. You could join to the hotdog table for rows where food is 'hotdog' and join to other tables for other specific

Polymorphic association foreign key constraints. Is this a good solution?

笑着哭i 提交于 2019-12-03 05:45:24
We're using polymorphic associations in our application. We've run into the classic problem: we encountered an invalid foreign key reference, and we can't create a foreign key constraint, because its a polymorphic association. That said, I've done a lot of research on this. I know the downsides of using polymorphic associations, and the upsides. But I found what seems to be a decent solution: http://blog.metaminded.com/2010/11/25/stable-polymorphic-foreign-key-relations-in-rails-with-postgresql/ This is nice, because you get the best of both worlds. My concern is the data duplication. I don't

Polymorphic associations in CakePHP2

妖精的绣舞 提交于 2019-12-03 03:25:26
I have 3 models, Page , Course and Content Page and Course contain meta data and Content contains HTML content. Page and Course both hasMany Content Content belongsTo Page and Course To avoid having page_id and course_id fields in Content (because I want this to scale to more than just 2 models) I am looking at using Polymorphic Associations. I started by using the Polymorphic Behavior in the Bakery but it is generating waaay too many SQL queries for my liking and it's also throwing an "Illegal Offset" error which I don't know how to fix (it was written in 2008 and nobody seems to have

how to avoid polymorphic associations

删除回忆录丶 提交于 2019-12-03 02:33:26
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(ActiveRecords) but I get into trouble when I'd switch to DM(DataMapper) or Sequel as both don't natevily support polymorphic associations and discourage it's usage. One workaround would be to use a big SQL clause with lot's of UNIONs to merge all the different tables which should

How to save entries in many to many polymorphic relationship in Laravel?

蹲街弑〆低调 提交于 2019-12-03 02:10:58
问题 I have an Org model and a Tag model. I want to associate tags with organizations. My database tables and Eloquent models are set up like so ... org id - integer name - string ... tags id - integer name - string taggables id - integer taggable_id - integer taggable_type - string // app/models/Org.php class Org extends Eloquent { protected $table = "org"; ... public function tags() { return $this->morphToMany('Tag', 'taggable'); } } // app/models/Tag.php class Tag extends Eloquent { protected

Rails - Polymorphic Favorites (user can favorite different models)

让人想犯罪 __ 提交于 2019-12-03 00:45:22
We are trying to add multiple favoritable objects, where a user can favorite many different objects, but are not sure how to make it work. Here is the Favorite model: class Favorite < ActiveRecord::Base # belongs_to :imageable, polymorphic: true belongs_to :user belongs_to :category belongs_to :business belongs_to :ad_channel belongs_to :location belongs_to :offer end The user model: class User < ActiveRecord::Base has_many :favorites, as: :favoritable end And one example model of something that can be favorited: class Category < ActiveRecord::Base has_many :sub_categories has_many :ad

has_many :through + polymorphic relationships

血红的双手。 提交于 2019-12-03 00:43:11
I using rails3 and trying to build some complex associations. I have Product, Version and Property models. class Version < ActiveRecord::Base belongs_to :product has_many :specs has_many :properties, :through => :specs end class Product < ActiveRecord::Base has_many :versions has_many :specs has_many :properties, :through => :specs end class Property < ActiveRecord::Base end class Spec < ActiveRecord::Base belongs_to :product belongs_to :spec belongs_to :version end It works perfect, but i want to use product and version as polymorphic relations, so table specs will have only spec_id and some

Route concern and polymorphic model: how to share controller and views?

早过忘川 提交于 2019-12-03 00:41:51
Given the routes: Example::Application.routes.draw do concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :forums do resources :forum_topics, concerns: :commentable end end And the model: class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end When I edit or add a comment, I need to go back to the "commentable" object. I have the following issues, though: 1) The redirect_to in the comments_controller.rb would be different depending on the parent object 2) The references on the views would differ as well = simple_form