polymorphic-associations

Rails: Many to many polymorphic relationships

百般思念 提交于 2019-12-03 00:33:39
问题 See comments for updates. I've been struggling to get a clear and straight-forward answer on this one, I'm hoping this time I'll get it! :D I definitely have a lot to learn still with Rails, however I do understand the problem I'm facing and would really appreciate additional help. I have a model called "Task". I have an abstract model called "Target". I would like to relate multiple instances of subclasses of Target to Task. I am not using single table inheritance. I would like to query the

Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

左心房为你撑大大i 提交于 2019-12-03 00:04:13
I'm trying to do a simple family reunion site with: "posts", "families", "kids", and "pictures". Ideally I'd like the routes/relationships to be structured this way: resources :posts do resources :pictures end resources :fams do resources :pictures resources :kids do resources :pictures end end In the models I have the necessary " belongs_to " and " has_many " relationships set between fams and kids . Fams , kids , and posts all are defined with "has_many :pictures, :as => :imageable " while pictures are defined as: belongs_to :imageable, :polymorphic => true When trying to do link_to "Edit"

Rails 3 polymorphic association with Carrierwave and Simple Form

孤者浪人 提交于 2019-12-02 21:31:09
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 => :attachable end image.rb class Image < ActiveRecord::Base belongs_to :attachable, :polymorphic =>

Polymorphic copy-constructor with type conversion

末鹿安然 提交于 2019-12-02 19:57:29
问题 I need to copy-construct an object simultaneously changing it's type to another class being a member of the same class-hierarchy. I've read about polymorphic copy-constructors and (hopefully) understand the idea behind it. Yet, I still don't know if this pattern applies to my case and, if so, how to implement it. I think it's best if I show what I need on an example. There is a Base class and two child classes, Child1 and Child2 . I need to create an object of type Child2 basing on Child1 ,

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

耗尽温柔 提交于 2019-12-02 14:12:52
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 $table = "tags"; public $timestamps = false; public function org() { return $this->morphedByMany('Org',

Rails: Many to many polymorphic relationships

橙三吉。 提交于 2019-12-02 14:08:33
See comments for updates. I've been struggling to get a clear and straight-forward answer on this one, I'm hoping this time I'll get it! :D I definitely have a lot to learn still with Rails, however I do understand the problem I'm facing and would really appreciate additional help. I have a model called "Task". I have an abstract model called "Target". I would like to relate multiple instances of subclasses of Target to Task. I am not using single table inheritance. I would like to query the polymorphic relationship to return a mixed result set of subclasses of Target. I would like to query

How do I get shoulda to recognise my polymorphic association

南楼画角 提交于 2019-12-02 11:29:51
问题 An almost indentical question to this has been asked before (How to use shoulda matchers to test a polymorphic assoication?) but there was no definitive answer that helps me, so I am trying again. I am using shoulda to test my associations and the following test fails require 'spec_helper' describe LabourEpidural do before {@treatment = FactoryGirl.build :treatment} subject {@treatment} it{should have_many :complications} end This fails with the following message Failure/Error: it{should have

How do I get shoulda to recognise my polymorphic association

二次信任 提交于 2019-12-02 04:42:52
An almost indentical question to this has been asked before ( How to use shoulda matchers to test a polymorphic assoication? ) but there was no definitive answer that helps me, so I am trying again. I am using shoulda to test my associations and the following test fails require 'spec_helper' describe LabourEpidural do before {@treatment = FactoryGirl.build :treatment} subject {@treatment} it{should have_many :complications} end This fails with the following message Failure/Error: it{should have_many :complications} Expected Treatment to have a has_many association called complications

Referencing foreign keys in the same column

喜欢而已 提交于 2019-12-02 03:28:53
问题 I'm designing a bd-scheme for mysql. My db stores 3 kinds of points: a, b or c and a path is composed by n pair of points: Route = [ (a1 or b1 or c1 ; a2 or b2 or c2), (a2 or b2 or c2 ; a3 or b3 or c3), ...] create table a_points ( point_id serial not null, owner_id bigint unsigned not null, name varchar(20) not null, primary key (point_id), foreign key (owner_id) references othertable (other_id) ) engine = InnoDB; create table b_points ( point_id serial not null, owner_id bigint unsigned not

Has_many through in Rails while assigning different roles

谁都会走 提交于 2019-12-02 02:56:42
问题 I'm trying to create a relationship between addresses and trips, and I'm not sure exactly how to set up the relationship. Each trip will have two addresses: the starting address and the ending address. Addresses can be used in many different trips, and they can either be the starting address or the ending address depending on the trip. What I envision is that when users are creating a new trip, they can select from a dropdown of all their addresses, so they could make a trip from their