Proper ActiveRecord modeling - ruby-on-rails

I have 3 different Models
Bid Order Printer
class Printer < ActiveRecord::Base
has_many :bids
end
class Order < ActiveRecord::Base
belongs_to :user
has_many :bids
end
class Bid < ActiveRecord::Base
belongs_to :printer
end
I would like to be able to view all of a printers orders they have bidded on, but with my current structure I cannot do it. it would be something like #printer.bids.orders
How can accomplish this? Would I have to change Order to have belongs_to Printer, or has_many: bids through orders? Something along those lines?

You would have to declare printer like this:
class Printer < ActiveRecord::Base
has_many :bids
has_many :orders, through: :bids
end
This should work

Related

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

Rails relationships setup for 5 table db

I'm stuck in a circle of indecision and need a shove to break free. I'm a noob with Rails and I'm attempting to translate a pre-existing database using Rails conventions. Basically, I currently have 5 models/tables: Workorders, Mileage_logs, Time_logs, Parts, & Equipment. A Workorder can have many Mileage_logs, Time_logs, and Parts, because each of them is shown on the Workorder's index page. But, that's the seemingly easy part...
I'm getting confused when the Equipment model is introduced because it seems like it's basically the same thing as the Workorder.
What is the best way to handle this relationship setup? Is this an appropriate use for the has_many :through convention? Or, is this best done with simply having the workorder_id and equipment_id in the Mileage_log, Time_log, and Part models and then:
class Part < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Mileage_log < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Time_log < ActiveRecord::Base
belongs_to :workorder
belongs_to :equipment
end
class Workorder < ActiveRecord::Base
has_many :Time_logs
has_many :Parts
has_many :Mileage_logs
end
class Equipment < ActiveRecord::Base
has_many :Time_logs
has_many :Parts
has_many :Mileage_logs
end
Or, is the has_many through: relationship what I should look into for the Workorder & Equipment models?
class Workorder < ActiveRecord::Base
has_many :parts
has_many :mileage_logs
has_many :time_logs
end
class Equipment < ActiveRecord::Base
has_many :parts, through: :workorder
has_many :mileage_logs, through: :workorder
has_many :time_logs, through: :workorder
has_many :workorders
end
Any help would be greatly appreciated!
Also, any general advice on the route setup would be welcomed as well.

Use where on has_many :through to find objects with 2 certain child object from different classes (Rails 4)

What I want to do is to get all the
My model:
class User < ActiveRecord::Base
has_many :classes
has_many :professors, :through=>:classes
has_many :cars
has_many :carmodels, :through=>:cars
end
class Professor < ActiveRecord::Base
has_many :classes
has_many :users, :through=>:classes
end
class Class < ActiveRecord::Base
belongs_to :user
belongs_to: professor
end
class Car < ActiveRecord::Base
belongs_to :user
belongs_to :carmodel
end
class Carmodel
has_many :cars
has_many :users, through=>:cars
end
what I want to do is, given a certain Car and Professor, to find all users which contain them.
for example
u1=carmodel.users
u2=professor.users
result=[]
u1.each do |us|
if u2.include? us
result.push us
end
end
Of course this is just an example... I would like to keep working with ActiveRecords(avoid turning it to an array) and, of course, a more optimal solution... I can't seem to find any.
You need to do something like this:
User.joins(:carmodels).joins(:professors)
Thanks for the previous answer! Here is the resulting code:
carmodel #Variable for the wanted carmodel
professor #Variable for the wanted professor
result=User.joins(:carmodels).joins(:professors).where(carmodel:{id:carmodel.id},professors:{id:professor.id})
Thanks again!

ActiveRecord Association for Grades App

A bit hard to explain with a title.
I am making a test grades app with Ruby on Rails, and can't figure out the best ActiveRecord Association setup.
Ideally: there are many Users, and there are many Tests. I need to store the each User's Scores from each Test. Right now I have this:
class User < ActiveRecord::Base
has_many :tests
has_many :scores, :through => :tests
end
class Test < ActiveRecord::Base
has_many :scores
end
class Scores < ActiveRecord::Base
belongs_to :users
belongs_to :tests
end
Doesn't seem right though. I would like to know the convention for this. Thanks.
I would use three tables/models:
test
user (or, possibly better, student)
test_result, having a test_id, user_id and score
Corresponding Ruby code:
class User < ActiveRecord::Base
has_many :tests, through: :test_results
end
class Test < ActiveRecord::Base
has_many :users, through: :test_results
end
class TestResult < ActiveRecord::Base
belongs_to :test
belongs_to :user
end

Resources