polymorphic-associations

Has_many through in Rails while assigning different roles

一世执手 提交于 2019-12-02 01:23:50
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 address called, say, "home" to their address called, say, "airport." There is already a polymorphic

How to handle an “OR” relationship in an ERD (table) design?

落花浮王杯 提交于 2019-12-01 18:11:34
问题 I'm designing a small database for a personal project, and one of the tables, call it table C , needs to have a foreign key to one of two tables, call them A and B , differing by entry. What's the best way to implement this? Ideas so far: Create the table with two nullable foreign key fields connecting to the two tables. Possibly with a trigger to reject inserts and updates that would result 0 or 2 of them being null. Two separate tables with identical data This breaks the rule about

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

泄露秘密 提交于 2019-12-01 05:45:21
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? 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 foreign key can reference only one target table. Another solution is to make a common "supertable" that both

many-to-many polymorphic relations

蹲街弑〆低调 提交于 2019-11-30 21:17:41
问题 I need to create a polymorphic relationship between Entity2, Entity1 and Types. the relationship between event and Types is easy to do, but tou have a problem in the relationship between Entity2 and Types, because it is a many-to-many relation. class CreateTypesTable extends Migration { public function up() { Schema::create('types', function(Blueprint $table) { $table->increments('id'); $table->integer('typeable_id'); $table->string('typeable_type', 20); $table->string('name', 20); $table-

Polymorphic Associations in Entity Framework

最后都变了- 提交于 2019-11-30 21:03:59
问题 I have a legacy database that has a few tables which were designed using Polymorphic Associations. By polymorphic associations, I mean that those tables can be child of different tables according to a column ObjectType . Example: Documents table has DocumentID (identity primary key), some other columns, and 2 special columns called ObjectType and ObjectID . If ObjectType='STUDENT' , ObjectID points to Students table. If ObjectType='TEACHER' , ObjectID points to Teachers table, etc. This is

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

家住魔仙堡 提交于 2019-11-30 18:16:16
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 column value so its corresponding table name cannot be put in the FROM/JOIN clauses of that query. The

Rails 5 - using polymorphic associations - rendering the views

陌路散爱 提交于 2019-11-30 17:06:15
问题 I'm trying to learn how to use polymorphic associations in my Rails 5 app. I recently asked this question, but I edited it so many times to show all the things I was trying, it has become messy I have models called Organisation, Proposal and Package::Bip. The associations are: Organisation has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true Proposal has_many :bips, as: :ipable, class_name: Package::Bip accepts

Rails Polymorphic has_many

旧巷老猫 提交于 2019-11-30 08:41:42
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 producer instance how can I get the collection of products if they are of varying types (per producer

rails 3 polymorphic association with paperclip and multiple models

空扰寡人 提交于 2019-11-30 07:21:54
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, :class_name => 'Attachment', :conditions => {:type => 'avatar'} accepts_nested_attributes_for :avatar

Inheritance and polymorphic-associations in rails

自古美人都是妖i 提交于 2019-11-30 05:33:04
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_type remains SomeProf . Why does this happen? Is there any way profile_type match subclass and not the