polymorphic-associations

RESTfully destroy polymorphic association in Rails?

自作多情 提交于 2019-12-11 02:46:02
问题 How do I destroy the association itself and leave the objects being associated alone, while keeping this RESTful? Specifically, I have these models: class Event < ActiveRecord::Base has_many :model_surveys, :as => :surveyable, :dependent => :destroy, :include => :survey has_many :surveys, :through => :model_surveys end class ModelSurvey < ActiveRecord::Base belongs_to :survey belongs_to :surveyable, :polymorphic => true end class Survey < ActiveRecord::Base has_many :model_surveys end That's

What is wrong with this polymorphic association?

主宰稳场 提交于 2019-12-11 02:44:05
问题 I have class User::AuthInfo < ActiveRecord::Base self.table_name = "auth_infos" belongs_to :authenticable, polymorphic: true end and class User::Customer < ActiveRecord::Base self.table_name = "customers" has_one :auth_info, as: :authenticable end I am expecting that by doing this: User::Customer.last.auth_info the output should be a record from auth_info . But I see the query is wrong: SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 8 LIMIT 1 My table has

Polymorphic removal of objects added to ObjectSet<TEntity> will NOT raise the IBindingList.ListChanged on ObjectSet<TEntity>.IListSource.GetList()

耗尽温柔 提交于 2019-12-10 16:16:28
问题 OVERVIEW/DESCRIPTION Simple: Polymorphic removal of objects of runtime types derived from TEntity added to an ObjectSet<TEntity> will NOT raise the IBindingList.ListChanged event on the IBindingList object returned by the of the ObjectSet<TEntity>.IListSource.GetList() method. However, removal of instances whose runtime type match TEntity are effectively notified on the ListChanged event. To clarify, at all times, objects are effectively removed from the underlying collections or data views

Laravel 4: save a polymorphic relationship's record: undefined method newQuery()

柔情痞子 提交于 2019-12-10 15:34:42
问题 I have a polymorphic relationship in Laravel 4 and it's working like I want. I have another one, which doesn't work when I try to insert the related model, even though it is identical to the first one and I use it in the same way. I have Event model and an Article model: // Article Model class Article extends Eloquent { public function events() { return $this->morphMany('Event', 'eventable'); } ... // Event Model class Event extends Eloquent { public function eventable() { return $this-

Ajax with Polymorphic Association (Destroy & Create)

两盒软妹~` 提交于 2019-12-10 11:59:57
问题 I have a polymorphic association with dailyposts & comments in my rails app Everything works fine in the database, but when I try to add Ajax to the Destroy Action (I used this tutorial http://railscasts.com/episodes/136-jquery-ajax-revised) ....it doesn't work unless I refresh the page. (but my alert works in destroy.js.erb) I know my error is in destroy.js.erb New to Rails...Please help :) This is my code... ROUTES resources :dailyposts do resources :comments end CONTROLLERS ##Dailyposts

Store child class's name in database column in polymorphic association - rails

穿精又带淫゛_ 提交于 2019-12-10 11:29:21
问题 I have a class, say, Car from which I have inherited classes Black and Red as follows: class Car::Black < Car end class Car::Red < Car end Now, there is another class, say, CarPurchase which has many Cars of both varieties. The associations are as follows: # In Black and Red models: has_many :car_purchases, as: :purchasable, dependent:destroy # In CarPurchase model: belongs_to :purchasable, polymorphic: true Now I'm trying to save the CarPurchases like this: black_car.car_purchases.new() #

Using Eloquent polymorphic relationships to categorise data in Laravel

不打扰是莪最后的温柔 提交于 2019-12-10 11:21:27
问题 In my application, I have a categories table, and multiple tables including services and articles that need to be categorised. For convenience, I want to use a polymorphic model for this data. A category is created to be used by either services or articles. Once a table has been defined as a services table, for example, it is not going to be displayed as a possible category to add articles to. This also applies to listing. I will have a menu for articles, and a menu for services, and each of

Can a single record from a polymorphic model belong to two (or more) models at the same time?

六眼飞鱼酱① 提交于 2019-12-10 10:46:18
问题 General newbie question: If I have a polymorphic model called Message , and two other models called Filter and User with has_many: messages, as ... association on both. Can a single record from Message belong to User and Filter models at the same time? For example, can I do: ... User.find(1).messages << Message.find(1) Filter.find(1).messages << Message.find(1) ... and have Message#1 available in in User#1 and Filter#1 ? The RailsGuides gives a very brief explanation, so this aspect is still

ActiveRecord::HasManyThroughAssociationPolymorphicSourceError

浪子不回头ぞ 提交于 2019-12-10 04:34:02
问题 I need a player to have many structures and the structure to belong to the player. Structure is a polymorphic relationship. class Player < ActiveRecord::Base has_many :player_structures has_many :structures, :through => player_structures end class PlayerStructures < ActiveRecord::Base belongs_to :structure, polymorphic: true belongs_to :player end class StructureA < ActiveRecord::Base has_one :player_structure, :as => :structure has_one :player, :through => :player_structure end class

rails joins polymorphic association

浪尽此生 提交于 2019-12-10 04:10:08
问题 I have a ploymorphic association named Notifiable in a model named Notifiaction : module Notifiable def self.included(base) base.instance_eval do has_many :notifications, :as => :notifiable, :inverse_of => :notifiable, :dependent => :destroy end end end class Bill < ActiveRecord::Base include Notifiable end class Balance < ActiveRecord::Base include Notifiable end class Notification belongs_to :notifiable, :polymorphic => true belongs_to :bill, foreign_key: 'notifiable_id', conditions: