I'm trying to setup a polymorphic association using the jsonapi-resources gem in Rails 5.
I have a User model that has a polymorphic association called profile, which can be of type Inspector or Buyer. Here are the truncated models:
class User < ApplicationRecord
belongs_to :profile, polymorphic: true
end
class Inspector < ApplicationRecord
belongs_to :user
end
class Buyer < ApplicationRecord
belongs_to :user
end
In the users table, there are corresponding profile_id and profile_type fields to represent the polymorphic association to inspectors and buyers. This all works as expected in our current Rails setup but I'm running into errors when trying to set this up for JSON:API using jsonapi-resources.
And now the corresponding jsonapi-resources resources and controllers (according to the directions):
class Api::V1::Mobile::UserResource < JSONAPI::Resource
immutable
attributes :name, :email
has_one :profile, polymorphic: true
end
class Api::V1::Mobile::ProfileResource < JSONAPI::Resource
end
class Api::V1::Mobile::ProfilesController < Api::V1::Mobile::BaseController
end
As far as I can tell, everything should now be setup properly but I get the following error when hitting the endpoint:
"exception": "undefined method `collect' for nil:NilClass",
"backtrace": [
".rvm/gems/ruby-2.6.5/gems/jsonapi-resources-0.10.2/lib/jsonapi/relationship.rb:77:in `resource_types'",
When digging into relationship.rb mentioned in the stack trace it looks like it can't get resolve the polymorphic types, so I tried the following:
class Api::V1::Mobile::UserResource < JSONAPI::Resource
immutable
attributes :name, :email
has_one :profile, polymorphic: true, polymorphic_types: ['inspector', 'buyer']
end
But alas, another error: Can't join 'User' to association named 'inspector'; perhaps you misspelled it?
Thanks in advance for any help with getting this setup!
The core problem actually has nothing to do with jsonapi-resources and is the associations. The inverse side of a belongs_to resource is always a has_one or has_many which points to the foreign key on the other table (and vice-versa).
class User < ApplicationRecord
belongs_to :profile, polymorphic: true
end
class Inspector < ApplicationRecord
has_one :user, as: :profile
end
class Buyer < ApplicationRecord
has_one :user, as: :profile
end
Having two belongs_to associations that point to each other would mean that you would have foreign keys on both sides - which is bad DB design due to the duplication (there should be only one source of truth) and won't really work in ActiveRecord since it will only ever write the foreign key on one side when you associate two models.
Related
I have a concern that creates an association:
# concerns/product.rb
module Product
extend ActiveSupport::Concern
included do
has_many :products, class_name: "Product", foreign_key: :owner_id
end
end
And an example model:
# models/user.rb
class User < ApplicationRecord
include Product
end
If I do: User.products.last it works fine. I also want to be able to do Product.last.owner but it won't work without the association being defined. I can't define it in the Product model since I have no clue to what models will include the concern that creates the association.
I have tried creating the association using inverse_of:
class Product < ApplicationRecord
belongs_to :owner, inverse_of: :products
end
... but it won't work, apparently it needs a class name.
I've also tried reflecting on which classes the concern gets included within but it raises some strange errors when the concern gets included into several different models.
How do I create the inverse of an association from within the concern?
As pointed out by #engineersmnky you can't actually setup an association that points to a bunch of different classes without using polymorphism:
# you can't have a class and a module with the same name in Ruby
# reopening the class/module will cause a TypeError
module ProductOwner
extend ActiveSupport::Concern
included do
has_many :products, as: :owner
end
end
class Product < ApplicationRecord
belongs_to :owner,
inverse_of: :products,
polymorphic: true
end
class User < ApplicationRecord
include ProductOwner
end
The information about what you're actually joining has to be stored somewhere on the products table. If you don't use polymorphism you just have an integer (or UUID) which will always refer to a single table.
I have a User model that can have an email and a phone number, both of which are models of their own as they both require some form of verification.
So what I'm trying to do is attach Verification::EmailVerification as email_verifications and Verification::PhoneVerification as phone_verifications, which are both STIs of Verification.
class User < ApplicationRecord
has_many :email_verifications, as: :initiator, dependent: :destroy
has_many :phone_verifications, as: :initiator, dependent: :destroy
attr_accessor :email, :phone
def email
#email = email_verifications.last&.email
end
def email=(email)
email_verifications.new(email: email)
#email = email
end
def phone
#phone = phone_verifications.last&.phone
end
def phone=(phone)
phone_verifications.new(phone: phone)
#phone = phone
end
end
class Verification < ApplicationRecord
belongs_to :initiator, polymorphic: true
end
class Verification::EmailVerification < Verification
alias_attribute :email, :information
end
class Verification::PhoneVerification < Verification
alias_attribute :phone, :information
end
However, with the above setup I get the error uninitialized constant User::EmailVerification. I'm unsure of where I'm going wrong.
How I structure this so that I can access email_verifications and phone_verifications on the User model?
When using STI you don't need (or want) polymorphic associations.
Polymorphic associations are a hack around the object-relational impedance mismatch problem used to setup a single association that points to multiple tables. For example:
class Video
has_many :comments, as: :commentable
end
class Post
has_many :comments, as: :commentable
end
class Comment
belongs_to :commentable, polymorphic: true
end
The reason they should be used sparingly is that there is no referential integrity and there are numerous problems related to joining and eager loading records which STI does not have since you have a "real" foreign key column pointing to a single table.
STI in Rails just uses the fact that ActiveRecord reads the type column to see which class to instantiate when loading records which is also used for polymorphic associations. Otherwise it has nothing to do with polymorphism.
When you setup an association to a STI model you just have to create an association to the base inheritance class and rails will handle resolving the types by reading the type column when it loads the associated records:
class User < ApplicationRecord
has_many :verifications
end
class Verification < ApplicationRecord
belongs_to :user
end
module Verifications
class EmailVerification < ::Verification
alias_attribute :email, :information
end
end
module Verifications
class PhoneVerification < ::Verification
alias_attribute :email, :information
end
end
You should also nest your model in modules and not classes. This is partially due to a bug in module lookup that was not resolved until Ruby 2.5 and also due to convention.
If you then want to create more specific associations to the subtypes of Verification you can do it by:
class User < ApplicationRecord
has_many :verifications
has_many :email_verifications, ->{ where(type: 'Verifications::EmailVerification') },
class_name: 'Verifications::EmailVerification'
has_many :phone_verifications, ->{ where(type: 'Verifications::PhoneVerification') },
class_name: 'Verifications::PhoneVerification'
end
If you want to alias the association user and call it initiator you do it by providing the class name option to the belongs_to association and specifying the foreign key in the has_many associations:
class Verification < ApplicationRecord
belongs_to :initiator, class_name: 'User'
end
class User < ApplicationRecord
has_many :verifications, foreign_key: 'initiator_id'
has_many :email_verifications, ->{ where(type: 'Verifications::EmailVerification') },
class_name: 'Verifications::EmailVerification',
foreign_key: 'initiator_id'
has_many :phone_verifications, ->{ where(type: 'Verifications::PhoneVerification') },
class_name: 'Verifications::PhoneVerification',
foreign_key: 'initiator_id'
end
This has nothing to do with polymorphism though.
I am new to rails and I'm using rails Polymorphic association and I ran into a problem
I have two models EmployeeTable and ProductTable. Both have some picture in Picture Model
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
class EmployeeTable < ActiveRecord::Base
has_many :pictures, as: :imageable
end
class ProductTable < ActiveRecord::Base
has_many :pictures, as: :imageable
end
there are two column in Picture imagable_id and imagable_type. When I create an entry in table it stores imagable_type as either "EmployeeTable" or "ProductTable". Can I change these name to any different Names like "employee" and "product".
Thanks in advance
To retreive the image_type without with table, you can add this method in your model. Changing the assosiated model name while saving will cause issues while querying
def neat_imageable_type
self.imageable_type.gsub("Table", "").downcase
end
I am trying to create a has_one association among two model.
class User < ActiveRecord::Base
has_one :emergency_contact
end
class EmergencyContact < ActiveRecord::Base
belongs_to :user
end
when i try to test it through rails console more than one entries are saved for the emergency contact model for a single user. Although when i retrieve it using User.emergency_contact only the first entry is returned. When saving how can i make it to rollback for more than one entry
You can simply validate uniqueness of user_id column in EmergencyContact:
class EmergencyContact < ActiveRecord::Base
belongs_to :user
validates_uniqueness_of :user_id, allow_nil: true
end
I'm using single table inheritance in my application and running into problems building inherited users from an ancestor. For instance, with the following setup:
class School < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
attr_accessible :type #etc...
belongs_to :school
end
Class Instructor < User
attr_accessible :terms_of_service
validates :terms_of_service, :acceptance => true
end
Class Student < User
end
How can I build either a instructor or student record from an instance of School? Attempting something like School.first.instructors.build(....) gives me a new User instance only and I won't have access to instructor specific fields such as terms_of_service causing errors later down the rode when generating instructor-specific forms, building from console will give me an mass-assignment error (as it's trying to create a User record rather than an Instructor record as specified). I gave the example of School, but there are a few other associations that I would like to inherit from the User table so I don't have to repeat code or fields in the database. Am I having this problem because associations can not be shared in an STI setup?
You should specify instructors explicitly
class School < ActiveRecord::Base
has_many :users
has_many :instructors,:class_name => 'Instructor', :foreign_key => 'user_id'
end
And what else:
class School < ActiveRecord::Base
has_many :users
has_many :instructors
end
class Instructor < User
attr_accessible :terms_of_service # let it be at the first place. :)
validates :terms_of_service, :acceptance => true
end
OK it seems part of the problem stemmed from having the old users association inside of my School model. Removing that and adding the associations for students and instructors individually worked.
Updated School.rb:
class School < ActiveRecord::Base
#removed:
#has_many :users this line was causing problems
#added
has_many :instructors
has_many :students
end