polymorphic-associations

Association for polymorphic belongs_to of a particular type

巧了我就是萌 提交于 2019-11-30 04:51:33
I'm relatively new to Rails. I would like to add an association to a model that uses the polymorphic association, but returns only models of a particular type, e.g.: class Note < ActiveRecord::Base # The true polymorphic association belongs_to :subject, polymorphic: true # Same as subject but where subject_type is 'Volunteer' belongs_to :volunteer, source_association: :subject # Same as subject but where subject_type is 'Participation' belongs_to :participation, source_association: :subject end I've tried a vast array of combinations from reading about the associations on ApiDock but nothing

Polymorphic Association with multiple associations on the same model

白昼怎懂夜的黑 提交于 2019-11-30 03:58:26
I'm slightly confused about a polymorphic association I've got. I need an Article model to have a header image, and many images, but I want to have a single Image model. To make matters even more confusing, the Image model is polymorphic (to allow other resources to have many images). I'm using this association in my Article model: class Article < ActiveRecord::Base has_one :header_image, :as => :imageable has_many :images, :as => :imageable end Is this possible? Thanks. Yep. That's totally possible. You might need to specify the class name for header_image , as it can't be inferred. Include

STI and polymorphs

点点圈 提交于 2019-11-30 02:49:57
问题 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 some NewsArticle i see in logs something like Comment Load (0.9ms) SELECT "comments".* FROM "comments" WHERE ("comments"."commentable_id" = 1 and "comments"."commentable

ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association

蹲街弑〆低调 提交于 2019-11-30 02:29:26
问题 class Transaction < ActiveRecord::Base belongs_to :account, :polymorphic => true end class Bankaccount < ActiveRecord::Base has_many :transactions, :as => :account end class Creditcard < ActiveRecord::Base has_many :transactions, :as => :account end Trying to do a summation of transactions where the account is active. Transaction.sum(:all, :conditions => "account.status = 'active'", :include => :account) So after some reading I came across this: The reason is that the parent model‘s type is a

Rails: has_many through with polymorphic association - will this work?

*爱你&永不变心* 提交于 2019-11-29 19:44:14
A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record? Here are the models I have: class Event < ActiveRecord::Base belongs_to :person belongs_to :eventable, :polymorphic => true end class Meal < ActiveRecord::Base has_one :event, :as => eventable end class Workout < ActiveRecord::Base has_one :event, :as => eventable end The main question concerns the Person class: class Person < ActiveRecord::Base has_many :events has_many :eventables, :through => :events # is this correct??? end

Rails 4 Polymorphic associations and concerns

你离开我真会死。 提交于 2019-11-29 13:33:09
I'm trying to add an Evaluation model to my Rails 4 app. I have made a model called evaluation.rb . It has: class Evaluation < ActiveRecord::Base belongs_to :evaluator, :polymorphic => true belongs_to :evaluatable, :polymorphic => true I have also made concerns for evaluator and evaluatable as: module Evaluator extend ActiveSupport::Concern included do has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation' end end module Evaluatable extend ActiveSupport::Concern included do has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name:

Rails Polymorphic has_many

三世轮回 提交于 2019-11-29 12:13:16
问题 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 Bicycles, or Popsicles, or Shoelaces. I can easily have each product class (Bicycle, Popsicle, etc.) have a belongs_to relationship to a Producer but given a

Polymorphic Assocations using Integer ID type fields

有些话、适合烂在心里 提交于 2019-11-29 09:52:56
I have a table Foo that has a polymorphic belongs_to association called bar . The foos table has the standard bar_id column. However, instead of a string-based bar_type column, I have an integer bar_type_id column. This column references the id column in the table bar_types . bar_types.name holds the name of the class that represents the class of the particular bar instance. Does Rails (ideally >=2.3.10) allow for this type of polymorphic association? We did it by overriding the association_class method in a new module and included it using the :extend option. Also created a integer to string

rails 3 polymorphic association with paperclip and multiple models

≯℡__Kan透↙ 提交于 2019-11-29 09:31:18
问题 I want to make polymorphic associations with paperclip, and allow my user to have one avatar and multiple images. Attachment model: class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true end class Avatar < Attachment has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" }, end class Image < Attachment has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" }, end User Model: has_one :avatar, :as => :attachable,

Inheritance and polymorphic-associations in rails

£可爱£侵袭症+ 提交于 2019-11-29 04:17:12
问题 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 SomeDeepProf2 < SomeProf Then: sdp1 = SomeDeepProf1.new user = sdp1.create_user user.profile_type > 'SomeProf' Even stating the association in subclasses, the profile