polymorphic-associations

Polymorphic controller and calling object

跟風遠走 提交于 2019-12-05 03:31:25
问题 I have a polymorphic relationship with address being able to both be owned by a member or a dependent. Everything looked great until I realized that I wouldn't know what type of object was creating it unless I'm missing something. Is there a way to tell the routing file to include the type of object? Models: class Member < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class Dependent < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class

How to enforce referential integrity on Single Table Inheritance?

一笑奈何 提交于 2019-12-04 23:57:05
I've read some of Bill Karwin's answers about single table inheritance and think this approach would be good for the setup I am considering: Playlist -------- id AUTO_INCREMENT title TeamPlaylist ------------ id REFERENCES Playlist.id teamId REFERENCES Team.id UserPlaylist ------------ id REFERENCES Playlist.id userId REFERENCES User.id PlaylistVideo ------------- id playlistId REFERENCES Playlist.id videoId REFERENCES Video.id All the CASCADE options are set to DELETE which will work correctly for when a Playlist is deleted, however, what happens if a User or Team is deleted? ie. If a User is

How to use constant in the ON condition in Yii2 hasMany relation

大憨熊 提交于 2019-12-04 22:18:55
I try to create a polymorphic association, what is common in Rails but unfortunately not in Yii2. As part of the implementation I need to define the relation: public function getImages() { return $this->hasMany(RecipeImage::className(), ['imageable_id' => 'id', 'imageable_type' => 'Person']); } But this doesn't work, because 'Person' is treated as an attribute of the current model, but it is a constant (class name for the polymorphic association). If I try to use 'andWhere' it adds the condition of course in a WHERE clause instead of the ON clause, causing that only records with existing image

Many-to-many connection and Associations

不羁岁月 提交于 2019-12-04 21:34:52
A User can Upload a Clip via the Clips Scaffold , this clip either belongs to a Show and or an Album (Show Controller, Album Controller). A user can have ONLY ONE SHOW MANY ALBUMS What i'm trying to accomplish: A User can Upload Clips , these clips can be attached to either a Show or/and an Album For a more detailed overview take a look at the image below: Problem #1 What i'm not understanding is the associations i must use. My current Models: User Model : has_many :clips, dependent: :destroy has_one :show, dependent: :destroy has_many :albums, dependent :destroy Clip Model : belongs_to :user

Eager Load Depending on Type of Association in Ruby on Rails

a 夏天 提交于 2019-12-04 18:41:46
问题 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 -%> <%=

Polymorphic habtm relationships with Rails/ActiveRecord

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 14:41:06
问题 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

How can I elegantly construct a form for a model that has a polymorphic association?

主宰稳场 提交于 2019-12-04 10:38:24
Here are my models: class Lesson < ActiveRecord::Base belongs_to :topic, :polymorphic => true validates_presence_of :topic_type, :topic_id end class Subject < ActiveRecord::Base has_many :lessons, :as => :topic end class Category < ActiveRecord::Base has_many :lessons, :as => :topic end Now, what I need is a form that will allow the user to create or update Lessons. The questions is, how can I provide a select menu that offers a mix of Subjects and Categories? (To the user, on this particular form, Subjects and Categories are interchangeable, but that's not the case elsewhere.) Ideally, this

polymorphic_path for custom collection route

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:31:20
I have the following route definition: resources :documents do collection do post :filter end end and the following model structure: class Document < ActiveRecord::Base belongs_to :documentable, :polymorphic => true end class User < ActiveRecord::Base has_many :documents, :as => :documentable end and controller structure: class DocumentsController < ApplicationController def index # not important end def filter # not important end end I can easily in a view say: polymorphic_path([@user, Document]) to get the path /users/1/documents , but I want to be able to say: filter_polymorphic_path([@user

Polymorphic controller and calling object

给你一囗甜甜゛ 提交于 2019-12-03 20:14:24
I have a polymorphic relationship with address being able to both be owned by a member or a dependent. Everything looked great until I realized that I wouldn't know what type of object was creating it unless I'm missing something. Is there a way to tell the routing file to include the type of object? Models: class Member < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class Dependent < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class Address < ActiveRecord::Base belongs_to :person, polymorphic: true end Controller: def new @person = ??

Finding all by Polymorphic Type in Rails?

倖福魔咒の 提交于 2019-12-03 17:19:44
问题 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) 回答1: There is no direct method for this. I wrote this monkey patch for ActiveRecord::Base . This