Having the following associations between 3 models:
workout.rb
class Workout < ActiveRecord::Base
has_and_belongs_to_many :workout_sets, :join_table => :workout_sessions
belongs_to :warmup, :class_name => :WorkoutStep, :foreign_key => "workout_step_id"
accepts_nested_attributes_for :workout_sets, allow_destroy: true
accepts_nested_attributes_for :warmup, allow_destroy: true
end
workout_set.rb
class WorkoutSet < ActiveRecord::Base
has_and_belongs_to_many :workout_steps, :join_table => :sets_steps, dependent: :destroy
has_and_belongs_to_many :workouts, :join_table => :workout_sessions
accepts_nested_attributes_for :workout_steps, allow_destroy: true
has_one :intro_video_usage, class_name: 'VideoUsage::Intro', as: :parent, dependent: :destroy
has_one :intro_video, through: :intro_video_usage, source: :video
accepts_nested_attributes_for :intro_video
has_one :get_ready_video_usage, class_name: 'VideoUsage::GetReady', as: :parent, dependent: :destroy
has_one :get_ready_video, through: :get_ready_video_usage, source: :video
has_one :congrats_video_usage, class_name: 'VideoUsage::Congratulations', as: :parent, dependent: :destroy
has_one :congrats_video, through: :congrats_video_usage, source: :video
end
and
workout_step.rb
class WorkoutStep < ActiveRecord::Base
has_and_belongs_to_many :workout_sets, :join_table => :sets_steps
has_many :main_video_usage, class_name: 'VideoUsage::Main', as: :parent
has_many :main_videos, through: :main_video_usage, source: :video
accepts_nested_attributes_for :main_videos
end
And using simple_form and cocoon to handle nested models creation on the top level model (Workout) I'm having troubles building the form for sets and steps - more concise, when associating a workout_set with an intro_video (and whitelisting the params) I'm having the following error:
Video(#70285207226600) expected, got String(#70285080848240)
The params object after sending looks like this:
"workout"=>{"title"=>"",
"workout_sets_attributes"=>{"0"=>{"_destroy"=>"false",
"intro_video"=>"70",
"title"=>""}}},
"image"=>"",
"sound_logo"=>"",
"intro_video"=>"",
"commit"=>"Create workout"}
Thanks in advance.
Your parameters are passing a string ("70") to intro_video= but association accessors like that expect you to pass an actual instance of the associated class (in this case Video).
You should instead be assigning to intro_video_id. The accessor will convert the string to an integer for you.
Related
I implemented a multi-table search using pg_search, and then serialize the result with Active Model Serializer (version 0.10) - it works fine, but AMS uses the default serializer for each of the types returned.
Here's the serializer:
class SearchBarSerializer < ApplicationSerializer
attributes :searchable_type
belongs_to :searchable
end
Thus, for example, when serializing the returned objects from pg_search, if the relevant object is a "User", then AMS uses UserSerializer. If the relevant type is a league, then AMS uses LeagueSerializer.
That's fine - but I would like to use a different serializer for each type. This is for a search bar, and so I only care about a much smaller amount of data than the full standard serializer. (EDIT: the standard serializers serialize all attributes and associations for each of the User and League models, which can be seen below. Each model is somewhat significantly large, and for the purposes of just a search, I really only need each model's name and id, and perhaps some other smaller data for each type)
Is there some way that I can specify which serializer to use depending on the object?
Thank you!
EDIT:
User Model:
class User < ActiveRecord::Base
include PgSearch
#################### Associations
has_and_belongs_to_many :roles
belongs_to :profile_page_visibility, optional: true # the optional part is just for when user's are created.
has_and_belongs_to_many :leagues, class_name: "Leagues::League", join_table: "users_leagues_leagues"
has_one :customer, class_name: "Payments::Customer", dependent: :destroy
has_many :unpaid_charges, class_name: "Payments::UnpaidCharge", dependent: :destroy
has_many :charges, class_name: "Payments::Charge", dependent: :destroy
has_many :cards, class_name: "Payments::Card", dependent: :destroy
has_many :league_join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
has_many :notifications, class_name: "Notification", foreign_key: :recipient_id
has_many :league_invitations, class_name: "Leagues::Invitation", dependent: :destroy
has_many :teams, class_name: "Leagues::Team"
has_many :divisions, class_name: "Leagues::Division" # Can act as division commissioner
has_many :conferences, class_name: "Leagues::Conference" # Can act as conference commissioner
League Model:
class Leagues::League < ApplicationRecord
enum pay_level: [ :basic, :custom, :premium ]
include PgSearch
#################### Associations
has_and_belongs_to_many :users, class_name: "User", join_table: "users_leagues_leagues"
has_and_belongs_to_many :commissioners, class_name: "User", join_table: "commissioners_leagues_leagues"
belongs_to :commissioner, class_name: "User", foreign_key: :commissioner_id, optional: true
has_and_belongs_to_many :feature_requests, class_name: "FeatureRequest", join_table: "feature_requests_leagues_leagues"
has_many :join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
has_many :invitations, class_name: "Leagues::Invitation", dependent: :destroy
has_many :notifications, class_name: "Notification", as: :notifiable_subject, dependent: :destroy
has_many :teams, class_name: "Leagues::Team", dependent: :destroy
has_many :conferences, class_name: "Leagues::Conference", dependent: :destroy
has_many :divisions, class_name: "Leagues::Division", dependent: :destroy
If you want to define a specific serializer lookup for your associations, you can override the ActiveModel::Serializer.serializer_for method to return a serializer class based on defined conditions.
For your case, it might look something like:
class SearchBarSerializer < ApplicationSerializer
attributes :searchable_type
belongs_to :searchable
class << self
def serializer_for(model, options)
return TinyUserSerializer if model.class == User
return TinyLeagueSerializer if model.class == Leagues::League
super
end
end
end
I have the association setup like the below, but I'm getting errors/exceptions thrown from Rails telling me that I don't have the associations set up properly.
Here's what I have:
class Case
has_many :case_accesses, as: :policy, inverse_of: :case, dependent: :destroy
has_many :agents, through: :case_accesses, source: :ownable, source_type: 'Agent'
end
class CaseAccess
belongs_to :policy, polymorphic: true
belongs_to :ownable, polymorphic: true
end
class Agent
has_many :case_accesses, as: :ownable, dependent: :destroy
has_many :cases, through: :case_accesses
end
The error from Rails:
Could not find the source association(s) "case" or :cases in model CaseAccess. Try 'has_many :cases, :through => :case_accesses, :source => '. Is it one of policy, connection, or ownable?
I tried setting the source to ownable and it's causing problems in my query. How should I be setting up this association? It's a traditional has many through, except on one side the policy can be either of type Case or Ppae, and the ownable can be of type Agent or User.
Columns for tables:
Case
-id
CaseAccess
-id
-policy_id
-policy_type
-ownable_id
-ownable_type
Agent
-id
A Case has many case_accesses as policy. Is the CaseAccess the policy, or is the Case the policy for the CaseAccess?
I believe you need to change
has_many :case_accesses, as: :policy, inverse_of: :case, dependent: :destroy
to
has_many :case_accesses, inverse_of: :policy, dependent: :destroy
This is what did it:
class Case
has_many :case_accesses, as: :policy, inverse_of: :policy, dependent: :destroy
has_many :agents, through: :case_accesses, source: :ownable, source_type: 'Agent'
has_many :users, through: :case_accesses, source: :ownable, source_type: 'User'
end
class CaseAccess
belongs_to :policy, polymorphic: true
belongs_to :ownable, polymorphic: true
end
class Agent
has_many :case_accesses, as: :ownable, dependent: :destroy
has_many :cases, through: :case_accesses, source: :policy, source_type: 'Case'
end
I have a User model:
class User < ActiveRecord::Base
has_many :tracks, dependent: :destroy
has_many :tracked_locations, through: :tracks, source: :tracking, source_type: 'Location'
and a Track model (think of it as 'following'):
class Track < ActiveRecord::Base
belongs_to :user
belongs_to :tracking, polymorphic: true
end
The idea here is I will have many models to track / follow so I am using polymorphism. For example I have a Location model:
class Location < ActiveRecord::Base
has_many :tracks, :as => :tracking, :dependent => :destroy
has_many :users, through: :tracks
Now in the console Location.first.users works fine along with User.first.tracked_locations.
Now I will be adding another polymorphic relationship along the lines of Flagged. The user can 'flag' another model with a note etc. So if I add has_many :users, through: :flagged to the Location model for example I need to differentiate between tracking users and flagged users.
I tried:
has_many :tracking_users, through: :tracks, source: :tracking, source_type: 'User'
but I get:
NoMethodError: undefined method `evaluators_for' for #<Location:0x007ff29e5409c8>
Can I even do this or am I missing something simple here?
UPDATE
Based on the answer below I figured it out:
has_many :tracking_users, through: :tracks, class_name: "User", foreign_key: "user_id", source: :user
I'm not 100% on this, but you could try:
has_many :tracking_users, through: :tracks, class_name: "User", foreign_key: "user_id", source: :user
Or you could also just create a class method and do it by hand.
def self.tracking_users
user_ids = tracks.collect(&:user_id)
User.where(id: user_ids)
end
edit: Had a brainfart, changed the "source" up there to :user. That tells what table to actually do the lookup in with the other attribute you've provided. of course it wouldn't be in :tracks
I have the following associations. PropertyOwner is a join model which belongs to a property and polymorphically belongs to an owner, which in the below example is a ForeclosureDefense. Everything works well, until I had the has_one :main_property. The idea is the ForeclosureDefense model can have many properties, but the last property is the main property:
class ForeclosureDefense < ActiveRecord::Base
has_many :property_owners, as: :owner
has_many :properties, through: :property_owners
has_one :main_property, through: :property_owners, source: :property, order: 'created_at desc'
end
class PropertyOwner < ActiveRecord::Base
belongs_to :property
belongs_to :owner, polymorphic: :true
end
class Property < ActiveRecord::Base
has_many :property_owners
has_many :owners, through: :property_owners
has_many :foreclosure_owners, through: :property_owners, source: :owner, source_type: "ForeclosureDefense"
has_many :folder_owners, through: :property_owners, source: :owner, source_type: "Folder"
end
Unfortunately, when I try to use that has_one :main_property association, I get the following error:
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'ForeclosureDefense#main_property' where the :through association 'ForeclosureDefense#property_owners' is a collection.
What am I doing wrong?
My solution was just to add it as a class-level macro:
def main_property
properties.order('created_at desc').first
end
I have 2 applications one that serves as an API and has read only access and one that is the primary application. In the primary app, I have a has many through polymorphic relationship. The models in the main app look like so, and they work great:
class Category < ActiveRecord::Base
has_many :category_associations
has_many :posts, through: :category_associations
has_many :pages, through: :category_associations
end
class Post < ActiveRecord::Base
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :post
end
class Page < ActiveRecord::Base
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :post
end
class CategoryAssociation
belongs_to :category
belongs_to :associated, polymorphic: true
end
Now for the second app I will need to access the same tables but my class names will be different, this effects the type field that I cannot seem to override even with source_type.:
class Category < ActiveRecord::Base
has_many :category_associations
has_many :articles, through: :category_associations
has_many :static_contents, through: :category_associations
end
class Article < ActiveRecord::Base
self.table_name = 'posts'
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :article, source_type: 'Post'
end
class StaticContent < ActiveRecord::Base
self.table_name = 'pages'
has_many :category_associations, as: :associated
has_many :categories, as: associated, through: :category_associations, source: :static_content, source_type: 'Page'
end
class CategoryAssociation
belongs_to :category
belongs_to :associated, polymorphic: true
end
I get the following Error:
=> Posts.first.categories
# ActiveRecord::HasManyThroughAssociationPointlessSourceTypeError: Cannot have a has_many :through association 'Post#categories' with a :source_type option if the 'CategoryAssociation#category' is not polymorphic. Try removing :source_type on your association.
It also seems that when I grab the posts from the category
Have you tried putting polymorphic:true to the category belongs_to? It seems that's the direction pointed by the error message points, though I'm not sure
class CategoryAssociation
belongs_to :category, polymorphic: true
...
end