Accessing one class from another using id as parameter - ruby-on-rails

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

Related

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

How to define a nested has_one association?

Suppose we have this contrived model structure
class Apple < ActiveRecord::Base
belongs_to :fruit
has_one :tree, through: :fruit
has_one :organism, through: :tree
end
class Fruit < ActiveRecord::Base
belongs_to :tree
has_many :apples
end
class Tree < ActiveRecord::Base
belongs_to :organism
has_many :fruits
end
class Organism < ActiveRecord::Base
has_many :trees
end
To avoid having to call #apple.fruit.tree.organism, I have definded the two has_one-through directives in Apple, and expect #apple.organism to work, but it does not. #apple.tree.organism does work.
Am I doing something wrong? Should I just define a getter method for :organism on Apple instances and be done with it?
Your has_one technically performs the characteristic of belongs_to: think Organism has_many apples through trees and not the other way around. "belongs_to :through" does not work in Rails, so I suggest using delegate in your models.
class Apple < ActiveRecord::Base
delegate :organism, to: :fruit
end
class Fruit < ActiveRecord::Base
delegate :organism, to :tree
end

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

Struggling with has_many :through

Suppose I have 3 Models like this (not sure if this is correct):
class User < ActiveRecord::Base
has_many :lessons
has_many :points, through: :progress
end
class Progress < ActiveRecord::Base
belongs_to :user
has_many :lessons
end
class Lesson < ActiveRecord::Base
belongs_to :progress
end
(The Progress table has user_id and lesson_id fields.)
How would I make it so calling #user.points would return the amount of entries into the Progress table. Also, how would I build a relationship?
class User < ActiveRecord::Base
has_many :progresses
has_many :lessons, through: :progresses
end
class Progress < ActiveRecord::Base
belongs_to :user
belongs_to :lesson
end
class Lesson < ActiveRecord::Base
has_many :progresses
end
First, you need to set up the association for progress on your User model, so that the through association will work:
class User < ActiveRecord::Base
has_many :lessons
has_many :progress
has_many :points, through: :progress
end
Then you'll need to define a method (or relation) of points on your Progress table. Or, if you simply want a count of records, you could do: #user.points.size

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