Ok, so I have this in my serializer and model:
class Review< ApplicationRecord
has_one :gamesection
has_many :releases, through: :gamesection
has_one :cover, through: :gamesection
has_many :otherpics, through: :cover, through: :gamesection
But the problem is that it renders like
Review
->gamesection
->release
->cover
->otherpics
While I want it to be
Review
->gamesection
--->release
--->cover
--->otherpics
How can I accomplish that?
You should have below in your rails serializer.
# /app/serializers/review_serializer.rb
class ReviewSerializer < ActiveModel::Serializer
attributes :id,
:title,
has_one :gamesection
end
# /app/serializers/gamesection_serializer.rb
class GamesectionSerializer < ActiveModel::Serializer
attributes :id,
has_many :releases
has_one :cover
has_many :otherpics
end
Related
I have a serializer for my Client model that causes an endless loop when I include has_many :referrers in the ClientSerializer.
class Client < ActiveRecord::Base
has_many :referrals, class_name: 'Referral', foreign_key: 'referrer_id'
has_many :referrals_as_referred, class_name: 'Referral', foreign_key: 'referred_id'
has_many :referred_clients, through: :referrals, source: :referred
has_many :referrers, through: :referrals_as_referred, source: :referrer
end
class Referral < ActiveRecord::Base
belongs_to :referrer, class_name: 'Client'
belongs_to :referred, class_name: 'Client'
end
In my serializer, the following works successfully:
class API::ClientSerializer < ActiveModel::Serializer
attributes :referrers
# has_many :referrers
has_many :referrals
def referrers
object.referrers
end
end
But this causes the infinite loop and stack error:
class API::ClientSerializer < ActiveModel::Serializer
has_many :referrers
has_many :referrals
end
Why is this occurring?
I'm making a rails marketplace app for uni where Users can be matched with specific products based on their request.
Users can list products that have specific categories.
Users can also list Requests where they can specify what products they're looking and their categories.
The aim is to match the request to a particular product based on the matching categories
Here are my models
class Product < ApplicationRecord
belongs_to :user
has_many_attached :images, dependent: :destroy
has_many :product_categories
has_many :categories, through: :product_categories
validates :user_id, presence: true
end
class Category < ApplicationRecord
has_many :product_categories
has_many :products, through: :product_categories
validates :name, presence: true, length: { minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
class ProductCategory < ApplicationRecord
belongs_to :product
belongs_to :category
end
class Request < ApplicationRecord
belongs_to :user
has_many_attached :images, dependent: :destroy
has_many :request_categories
has_many :categories, through: :request_categories
validates :user_id, presence: true
end
class RequestCategory < ApplicationRecord
belongs_to :request
belongs_to :category
end
I was thinking of creating a new model called Match to bring together the product and categories or is it easier to match it in the request?
In my mind, your new Match class would essentially be a join table for a has_many :through association. Assuming that you're implementing an asynchronous worker (e.g. Sidekiq / ActiveJob) to go through and make "matches", you'll want to connect matches to a particular Request, and likely store some meta-data (has the user seen the Match yet? Have they rejected it?)
So, I'd probably generate a Match class like this:
rails g model Match seen_at:datetime deleted_at:datetime request:references product:references
And set up the associations as follows:
class Match < ApplicationRecord
belongs_to :request
belongs_to :product
end
class Request < ApplicationRecord
belongs_to :user
has_many_attached :images, dependent: :destroy
has_many :request_categories
has_many :categories, through: :request_categories
has_many :matches
has_many :products, through: :matches
validates :user_id, presence: true
end
class Product < ApplicationRecord
belongs_to :user
has_many_attached :images, dependent: :destroy
has_many :product_categories
has_many :categories, through: :product_categories
has_many :matches
has_many :requests, through: :matches
validates :user_id, presence: true
end
Also, you'll likely want to add the Request has_many :through to your Category model (I think you forgot that one):
class Category < ApplicationRecord
has_many :product_categories
has_many :products, through: :product_categories
has_many :request_categories
has_many :requests, through: :request_categories
validates :name, presence: true, length: { minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
The big part of the job is working out how to have your app periodically look for matches - you may want to start with the Active Job Basics documentation.
Here are my relevant models:
class ListItem < ActiveRecord::Base
belongs_to :inventory_item
belongs_to :shopping_list
belongs_to :item
end
class ShoppingList < ActiveRecord::Base
has_many :list_items
belongs_to :user, :foreign_key => :user_id
end
class InventoryItem < ActiveRecord::Base
belongs_to :item, :foreign_key => :item_id
belongs_to :vendor
has_many :list_items
end
I'm trying to access attributes of InventoryItem in my view. Here's what I currently have in my ShoppingListController.
def show
#list_items = ShoppingList.find(params[:id]).list_items
end
Can I do something like #inventory_items = #list_items.inventory_items? That code and variants of it that I've tried haven't worked. What am i missing here? Any tips for accessing attributes through multiple models like this? Thanks in advance!
The most straight forward approach would be to use has_many through on the ShoppingList class:
has_many :inventory_items, through: :list_items
Here is my three models/tables
class Swimming::Classschedule < ActiveRecord::Base
belongs_to :swimming_classtimes ,:class_name=>'Swimming::Classtime',:foreign_key => "classtime_id"
attr_accessible :id,:coach_id, :level_id, :note, :classtime_id
end
class Swimming::Classtime < ActiveRecord::Base
has_many :swimming_classschedules,:class_name=>'Swimming::Classschedule'
belongs_to :swimming_timeblocks ,:class_name=>'Swimming::Timeblock',:foreign_key => "timeblock_id"
attr_accessible :date, :end, :start,:timeblock_id,:id
end
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
attr_accessible :name,:id
end
swimming_classschedules belongs_to swimming_classtimes
swimming_classtimes belongs_to swimming_timeblocks
swimming_timeblocks has_many swimming_classtimes
swimming_classtimes has_many swimming_classschedules
How are swimming_classschedules and swimming_timeblocks associated ?
swimming_timeblocks has_many swimming_classschedules through swimming_classtimes
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
has_many :swimming_classschedules,:through => :swimming_classtimes
attr_accessible :name,:id
end
I have in form this code line <%= f.collection_select :owner_ids, Owner.order(:id), :id, :name, {}, {multiple: true} %> that returns the list of owners in the massmedium form, but I need in this field also to include the companies too, so I could have in the same list the companies too, cause a massmedia chanel could be owned by a person or by a company.
Company.rb
class Company < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :owners, through: :ownerships
end
Owner.rb
class Owner < ActiveRecord::Base
has_many :ownerships
has_many :massmedia, through: :ownerships
has_many :companies, through: :ownerships
end
Massmedium.rb
class Massmedium < ActiveRecord::Base
belongs_to :category
has_many :ownerships
has_many :owners, through: :ownerships
has_many :companies, through: :ownerships
end
= f.select :owners, options_for_select((Owner.all + Company.all)
.collect{|o| [o.name, "#{o.class},#{o.id}"], {multiple: true}
u have to define owners=(values) method in your model, which will parse values like ["Company,1","Owner,2"]