how to use has many through with condition in rails - ruby-on-rails

I have following models in rails.
class User < ApplicationRecord
has_many :vendors
has_many :vendoritems, through: :vendors
has_many :products
end
class Vendorcode < ApplicationRecord
has_many :vendoritems
end
class Vendoritem < ApplicationRecord
belongs_to :vendorcode
belongs_to :vendor
end
class Vendor < ApplicationRecord
belongs_to :user
has_many :vendoritems
end
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
has_many :vendoritems, XXXXX
end
Product has many vendoritems through vendorcode and user.
How can I implement this association.

I'd just go for an instance method like so
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems
end
end
Cheers!

class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems.where('vendorcode =?', vendorcode.id)
end
end
I solved the problem.

Related

trying the rails model association

I'm not sure if this a best practices in terms of association. Anyone can help me
//post,rb
class Post < ApplicationRecord
belongs_to :user
has_one :location, through: :user
has_one :category, through: :user
has_one :type, through: :user
end
//user.rb
class User < ApplicationRecord
has_many :posts
end
//category.rb
class Category < ApplicationRecord
belongs_to :post
end
//location.rb
class Location < ApplicationRecord
belongs_to :post
end
//type.rb
class Typo < ApplicationRecord
belongs_to :post
end
So the one of main goal of this are it's like
User.posts.location,create(country: "Japan", city: "Kyoto")
but i get an error with location NoMethodError: undefined method `location' for #
also should i a references in post like location:references type:references category:references
You need to rename the classes like
#location.rb
class Location< ApplicationRecord
belongs_to :post
end
#type.rb
class Type < ApplicationRecord
belongs_to :post
end
Not need to
through: :user #=> this use for many to many relationship
You can remove this

Find records that have two exact instances of a many-to-many relation

I have an application with a business logic concerning products with multiple variants:
class Task < ApplicationRecord
belongs_to :variant
end
class Variant < ApplicationRecord
belongs_to :product
has_many :variant_option_values
has_many :option_values, through: :variant_option_values
has_many :prices
end
class Product < ApplicationRecord
has_many :product_option_types
has_many :option_types, through: :product_option_types
has_many :variants
end
class OptionValue < ApplicationRecord
belongs_to :option_type
end
class OptionType < ApplicationRecord
has_many :product_option_types
has_many :products, through: :product_option_types
has_many :option_values
end
class ProductOptionType < ApplicationRecord
belongs_to :product
belongs_to :option_type
end
class VariantOptionValue < ApplicationRecord
belongs_to :variant
belongs_to :option_value
end
The ERD looks like this:
Having a product product_1 how can I find its variants that have OptionValue instances option_value_1, option_value_2 and option_value_3? Note that the variant has to have all three option values at the same time and can have more than those three (but not necessarily).
option_values = [option_value_1, option_value_2, option_value_3]
Variant.include(product: [option_types: :option_values])
.where("option_values.id IN (?)", option_values.map(&:id))
.group("products.id")
.having("count(*) >= ?", option_values.size)

Rails - search through has_many association

I have these models:
class Car < ActiveRecord::Base
has_many :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_definition
end
class CarServiceDefinition < ActiveRecord::Base
has_many :car_services
end
I am trying to find out if the currently selected car has a certain service - trying to do it this way:
airbag = car.car_services.car_service_definitions.where('service_type = "Airbag"').first
But this query doesn't work because of the wrong using model associations.
How do I find out, if the current car has some airbags?
Thank you in advance.
Assuming your migrations are fine
class Car < ActiveRecord::Base
has_many :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_list
has_and_belongs_to_many :car_service_definitions
end
class CarServiceDefinition < ActiveRecord::Base
end
airbag = car.car_services.car_service_definitions.where(service_type: 'AirBag').first
Well, from the look of the relationships, I assume that car_services is the rich join table of cars and car_service_definitions
What you can do is to set up has_many :through relationship on both car and car_service_definition
class Car < ActiveRecord::Base
has_many :car_services
has_many :car_service_definitions, through: :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_definition
end
class CarServiceDefinition < ActiveRecord::Base
has_many :car_services
has_many :cars, through: :car_services
end
And then if you want to find airbag, it would be like this
airbag = car.car_service_definitions.where("car_service_definitions.service_type" => 'AirBag').first
But if you want to check if the car has air_bag, could just write a method like this
class Car < ActiveRecord::Base
def has_air_bag?
car_service_definitions.where("car_service_definitions.service_type" => 'AirBag').count > 0
end
end

Connect the relationship between category,subcategory and gig

My application has categories,than subcategories,than gigs
I have gigs_controller.rb in my controller file
And category.rb, subcategory.rb, gig.rb in my Model file.
How do i make their connection with each other,following the principle of category=subcategory=gig(by gig i mean i lot of small adds)
Will it be right to say in Category.rb model
class Category < ActiveRecord::Base
has_many :subcategories
end
in my Subcategory Model
class Subcategory < ActiveRecord::Base
belongs_to :category
end
and in my Gig.rb
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :category # I also need here to be belongs_to :subcategory,how do i do that?
end
Should i create a controller for category and subcategory ?, how would you go about that.
Thank you,for your support,hope my question will help others as well.
class Category < ActiveRecord::Base
has_many :subcategories
end
class Subcategory < ActiveRecord::Base
belongs_to :category
has_many :gigs
end
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :subcategory
end
Yes you can use has_many in model without having a controller.Because has_many uses table's name.
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :subcategory
end
class Category < ActiveRecord::Base
has_many :subcategories
end
class Subcategory < ActiveRecord::Base
belongs_to :category
has_many :gigs
end

Model associations for a contest

I'm new to rails and working on an app that has the following situation:
Users have skills (e.g rafting, dancing)
Users participate in contests
Contest measures multiple skills
At the end of each contest, each user gets a score (e.g dancing: 5, rafting: 4)
Whats the best way to model this ?
Thanks,
This got nasty :s At the end I was actually not sure if this is the right way
class Skill < ActiveRecord::Base
has_many :skill_scores
has_many :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
class SkillScore < ActiveRecord::Base
belongs_to :user
belongs_to :contest
belongs_to :skill
end
class User < ActiveRecord::Base
has_many :skills
has_many :contests, :through => :contest_participations
has_many :skill_scores
end
class Contest < ActiveRecord::Base
has_many :users, :through => :contest_participations
has_many :skill_scores
end
class ContestParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :contest
end

Resources