Rails - search through has_many association - ruby-on-rails

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

Related

how to use has many through with condition in 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.

Accessing one class from another using id as parameter

The association between class are as below
class Level < ActiveRecord::Base
has_many :levels_users
end
class LevelsUser < ActiveRecord::Base
belongs_to :level
has_many :schedules
end
class Schedule < ActiveRecord::Base
belongs_to :levels_user
end
I know the value of level.id. How do I find all schedules belonging to that level.id. level.id is the id of one of the level. I tried something like this:
Level.find(level.id).levels_users.schedule
But it won't work.
You could use through property of has_many association.
class Level < ActiveRecord::Base
has_many :levels_users
has_many :schedules, through: :levels_users
end
Now you can use
level.schedules

Activerecord proper associations

I have four models
Bid Order User Printer
currently I cannot access the order through anything except User.
I would like to be able to do something of the sort bid.order but have yet to figure out the correct association. Any thoughts?
class Bid < ActiveRecord::Base
belongs_to :printer
end
class Order < ActiveRecord::Base
belongs_to :user
has_many :bids
end
class Printer < ActiveRecord::Base
has_many :orders, through: :bids
has_many :bids
end
class User < ActiveRecord::Base
has_many :orders
end
Both models must know about the relationship so you need to state that in the Bid model
class Bid < ActiveRecord::Base
belongs_to :printer
belongs_to :order
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

How to get value of some column of association table in Rails?

how can I get a weight of product through a Record model? As I know, possible to get all products of certain record but I cannot find out the way getting the weight of certain product.
class User < ActiveRecord::Base
has_many :eatings
end
class Eating < ActiveRecord::Base
belongs_to :user
has_many :records
end
class Record < ActiveRecord::Base
belongs_to :eating
end
class Product < ActiveRecord::Base
end
class WeightedProduct < ActiveRecord::Base
end
What relationships should have Record and Product models with WeightedProduct so user will be able to get weight of certain product through one line User.first.eatings.first.records.first.products.first.weight?
Looks like you're after this:
class Record < ActiveRecord::Base
belongs_to :eating
has_many :weighted_products
end
class Product < ActiveRecord::Base
has_many :weighted_products
end
class WeightedProduct < ActiveRecord::Base
belongs_to :record
belongs_to :product
end
Then User.first.eatings.first.records.first.weighted_products.first.weight
I think that should work but haven't tested.
it seems that each product has one weighted product, then in that case you should add
class Product < ActiveRecord::Base
has_one :weighted_product
end
class WeightedProduct < ActiveRecord::Base
belongs_to :product
end
and
class Record < ActiveRecord::Base
belongs_to :eating
has_many :products
end

Resources