polymorphic-associations

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

折月煮酒 提交于 2019-12-20 21:23:26
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

Polymorphic associations in CakePHP2

一个人想着一个人 提交于 2019-12-20 13:52:12
问题 I have 3 models, Page , Course and Content Page and Course contain meta data and Content contains HTML content. Page and Course both hasMany Content Content belongsTo Page and Course To avoid having page_id and course_id fields in Content (because I want this to scale to more than just 2 models) I am looking at using Polymorphic Associations. I started by using the Polymorphic Behavior in the Bakery but it is generating waaay too many SQL queries for my liking and it's also throwing an

how to avoid polymorphic associations

£可爱£侵袭症+ 提交于 2019-12-20 12:21:24
问题 Given you have to implement a news feed like the one seen in social networks, ex facebook. Currently I'm using a News class which has a polymorphic assocation which can be of any kind like Image, Comment, Friendship, GroupMembership, etc. Whenever an Object is created, as News is created too. It's working fine with AR(ActiveRecords) but I get into trouble when I'd switch to DM(DataMapper) or Sequel as both don't natevily support polymorphic associations and discourage it's usage. One

Using polymorphic paths with nested associations

只谈情不闲聊 提交于 2019-12-20 09:38:50
问题 I have a polymorphic association that looks like this: class Line < ActiveRecord::Base belongs_to :item, :polymorphic => true end class Education < ActiveRecord::base has_many :lines, :as => :item end class Work < ActiveRecord::base has_many :lines, :as => :item end I'd like a simple way to create a new Line from the parent Item. So, I might be editing a view for a Work object, and want to have a link that creates a new Line object. Normally, I would do this: <%= link_to "New Line", new_work

MySQL: Two n:1 relations, but not both at once

戏子无情 提交于 2019-12-19 07:32:45
问题 Sorry for the title, it's difficult to explain. I need a data model similar to this: As you can see, a set can belong to both a user or a school. My problem: It should only be allowed to belong either to a user OR a school. But never both at the same time. How can I solve this problem? 回答1: Your current design is called exclusive arcs where the sets table has two foreign keys, and needs exactly one of them to be non-null. This is one way to implement polymorphic associations, since a given

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

夙愿已清 提交于 2019-12-18 10:06:30
问题 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 <

Rails 4 Polymorphic associations and concerns

泄露秘密 提交于 2019-12-18 07:43:47
问题 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:

ActiveRecord, has_many :through, and Polymorphic Associations

泪湿孤枕 提交于 2019-12-17 02:53:06
问题 Folks, Want to make sure I understand this correctly. And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. That said, consider the following... class Widget < ActiveRecord::Base has_many :widget_groupings has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'" has_many :aliens, :through => :widget_groupings, :source => :alien,

MySQL - Conditional Foreign Key Constraints

心不动则不痛 提交于 2019-12-17 00:46:18
问题 I have following 'comments' table in my app: comments -------- id INT foreign_id INT model TEXT comment_text TEXT ... the idea of this table is to store comments for various parts of my app - it can store comments for blog post i.e: 1|34|blogpost|lorem ipsum... user picture: 2|12|picture|lorem ipsum... and so on. now, is there a way to force FOREIGN KEY constraint on such data? i.e. something like this in comments table: FOREIGN KEY (`foreign_id`) REFERENCES blogposts (`id`) //but only when

Laravel - Eloquent: Polymorphic relations with namespace

ε祈祈猫儿з 提交于 2019-12-14 03:59:54
问题 My situation is: a Calendar belongs to a Customer or Salesman Because I also have classes like Event and File, I used the namespace App\Models for all my model classes. so I set up the polymorphic relation: in Calender.php public function user() { return $this->morphTo(); } in Customer.php and Salesman.php public function calendars() { return $this->morphMany('App\Models\Calendar', 'user'); } Now when i do $calendar= Calendar::find(1); //calendar from a salesman $calendar->user; //error here