polymorphic-associations

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

送分小仙女□ 提交于 2019-12-03 16:32:19
问题 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

Creating forms for polymorphic associations in Rails

空扰寡人 提交于 2019-12-03 16:07:30
问题 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 new movie-comment? I added resources :movies do resources :comments end to my routes.rb, and tried new_movie_comment_path(@movie), but this gives me a form containing

Eager loading of polymorphic associations in ActiveRecord

亡梦爱人 提交于 2019-12-03 13:22:51
问题 This is my first time using Rails and I was wondering if it's possible to load a has one polymorphic association in one SQL query? The models and associations between them are basic enough: An Asset model/table can refer to a content (either an Image, Text, or Audio) through a polymorphic association, i.e. class Asset < ActiveRecord::Base :belongs_to :content, :polymorphic => true end and the Image, Text, Audio are defined like this: class Image < ActiveRecord::Base :has_one :asset, :as =>

Should I joint-index an ActiveRecord polymorphic association?

半城伤御伤魂 提交于 2019-12-03 11:53:41
I have a metric table that I expect to be very large. It has a polymorphic association so that it can belongs_to other models that want to record some metric. I typically index association columns like this to speed up association loading. I've heard people talking about joint-indexing this association . This looks like: add_index :comments, [:commentable_type, :commentable_id] But I've also heard counsel against creating indexes of low-cardinality , because the payoff of the index doesn't offset the overhead of maintaining it. Since the _type half of my polymorphic association will probably

Eager Load Depending on Type of Association in Ruby on Rails

你离开我真会死。 提交于 2019-12-03 11:13:12
I have a polymorphic association ( belongs_to :resource, polymorphic: true ) where resource can be a variety of different models. To simplify the question assume it can be either a Order or a Customer . If it is a Order I'd like to preload the order, and preload the Address . If it is a customer I'd like to preload the Customer and preload the Location . The code using these associations does something like: <%- @issues.each do |issue| -%> <%- case issue.resource -%> <%- when Customer -%> <%= issue.resource.name %> <%= issue.resource.location.name %> <%- when Order -%> <%= issue.resource

Rails - Polymorphic Favorites (user can favorite different models)

ぐ巨炮叔叔 提交于 2019-12-03 10:14:44
问题 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

has_many :through + polymorphic relationships

本小妞迷上赌 提交于 2019-12-03 10:14:10
问题 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

Polymorphic habtm relationships with Rails/ActiveRecord

别等时光非礼了梦想. 提交于 2019-12-03 09:11:51
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 CustomerStore and SoftwareSystem would be of type "Targetable" in this circumstance. From what I understand, if I implement the polymorphic relationship as most examples show, I'd only be able to relate a Targetable to a Task once . Some clarification might help as most

Rails 3 polymorphic association with Carrierwave and Simple Form

允我心安 提交于 2019-12-03 08:02:01
问题 I'm trying to set up a polymorphic association for photo uploads which are processed using Carrierwave. I'm using Simple Form to build my forms. I feel like the association is correct so I'm wondering if my problem is just something with the form or controller. Here are my associations: property.rb: class Property < ActiveRecord::Base attr_accessible :image ... has_many :image, :as => :attachable ... end unit.rb class Unit < ActiveRecord::Base attr_accessible :image ... has_many :image, :as =

Database best practices

烂漫一生 提交于 2019-12-03 07:23:37
I have a table which stores comments, the comment can either come from another user, or another profile which are separate entities in this app. My original thinking was that the table would have both user_id and profile_id fields, so if a user submits a comment, it gives the user_id leaves the profile_id blank is this right, wrong, is there a better way? In the past I have used a centralized comments table and had a field for the fk_table it is referencing. eg: comments(id,fk_id,fk_table,comment_text) That way you can use UNION queries to concatenate the data from several sources. SELECT c