I have this associations:
class Ship < ApplicationRecord
has_many :captain_profiles
has_many :captains, through: :captain_profiles
end
class CaptainProfile < ApplicationRecord
belongs_to :captain
belongs_to :ship
end
class Captain < ApplicationRecord
has_one :captain_profile
has_many :schedules
end
class Schedule < ApplicationRecord
belongs_to :captain
end
And I need list of all ships that are ready to be taken to the sea. In other words I have to find all ships that has at least one Captain with at least one of his schedules.
I thought about merging two inner joins as I need Ships which has Captains which has Schedules.
I tried Captain.includes(:schedules).where("schedule.id IS NOT NULL") and so with Ships but it does not work. Could someone explain me what am I doing wrong and how shall I do this?
Simply use joins which generates INNER JOIN.
Ship.joins(captains: :schedules)
Related
I have a table called foods and categories, but this table has no relation, I want to connect them through another table called food_category. And I want to make one-to-one relation between food and category maybe the diagram looks like this
class Food < ApplicationRecord
has_one :category
end
class Category < ApplicationRecord
has_one :food
end
class FoodCategory < ApplicationRecord
belongs_to :category
belongs_to :food
end
Is it possible to do this?
Yes, this is possible. You just need to do
has_one :category, through: :food_categories
as discussed in the Rails docs here.
However, this is a long-winded way to go about this kind of association. If it's going to be one-to-one, why not just add a foreign key to Category from Food? And presumably, you would actually want Category to contain many Food records? Seems like the below would make more sense:
class Food < ApplicationRecord
belongs_to :category
end
class Category < ApplicationRecord
has_many :food
end
class Food < ApplicationRecord
has_one :food_category
has_one :category, through: :food_categories
end
In rails console, you can access like this
Food.find(:id).categories
I have 3 models:
sophead
od
od_item
sophead has many ods and
od has many od_items.
each od_item belongs to one od and each od belongs to one sophead
I want to be able to return all od_items for a specific sophead like this:
all_od_items_for_first_sophead = Sophead.first.od_items
what is the correct association for getting all the od_items for a sophead?
I had tried:
has_many :od_items, through: :ods
but I believe that this is incorrect as it doesn't really match this diagram - in that diagram's example (with different model names), the arrow from patients to appointments would run the other direction.
Thanks in advance
Your try was correct way to do it.
Your models must have associations like this:
models/sophead.rb
class Sophead < ApplicationRecord
has_many :ods
has_many :od_items, through: :ods
end
models/od.rb
class Od < ApplicationRecord
belongs_to :sophead
has_many :od_items
end
models/od_item.rb
class OdItem < ApplicationRecord
belongs_to :od
end
Happy New Year to everyone!
I have classic has_many through association:
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
However now I need to add has_many association from class Appointment to some class Example, which is in belongs_to association with another model and would be in belongs_to association with class Example.
If possible, how to to set this kind of assosiation? Thank you.
Update
I don't understand, why this question is downvoted.
Here is what I need in class Example :
class Example < ApplicationRecord
belongs_to :appointment
belongs_to :model_bbb
end
Update 2
Ok, I figured out I can use solution from this answer. Basically I can drop "Appointment" model and have class Example like this:
class Example < ApplicationRecord
belongs_to :physician
belongs_to :patient
belongs_to :model_bbb
end
Then in Physician and Patient I can do has_many :examples and another through relationship. I did wanted to do some strange belongs_to through thing as I could have relatively small table of class Appointment, but class Example table is expected to be quite big. So my initial thinking was not to create extra column which will be duplicated for so many times.
You could just add a has_many or has_many through to your Appointment model class.
Keep in mind that belongs_to association is always on the table that has the foreign key. So if you're right at your modeling you should have an appointment_id, for instance, on the examples table.
There's no problem using the associative table in relationship with another. Actually the idea of using an intermediate table is to be able to store other information on it (otherwhise you would be doing HABTM).
If could really help me out with problem I have, I would really appreciate it. My problem is that I can not select attributes from certain tables that have no association between them.
To explain myself better, here are my models:
class User < ActiveRecord::Base
has_many :measurement_blocks
has_many :measurements, through: :measurement_blocks
end
class MeasurementBlock < ActiveRecord::Base
belongs_to :user
has_many :measurements
end
class Measurement < ActiveRecord::Base
belongs_to :measurement_block
end
class Device < ActiveRecord::Base
has_many :measures
end
class Measure < ActiveRecord::Base
belongs_to :device
end
I can get a ProxyAssociation with
user.measurements
but what I really want is to include the device and measures names.
I have tried the following:
measurements = user.measurements
measurements.include("INNER JOIN ON devices (devices.id = measurements.device_id)")
but it does not work. Furthermore, as I mentioned before I want to
include the measures and devices names along with the measurements.
ActiveRecord is frustrating to me when there is no full association between models.
Thank you again.
hey guys
I want to build a database and some association between them, below are some descriptions
Drummer and Video are many-to-many association, because, sometimes, more than one drummer will appear on one video clip
Cymbal and Video are only many-to-many association, same reason
Event and Video are one-to-many, because it makes sense that one video only represent only one Event
so for the first two my solution is using two has-and-belongs-to-many association sign to the both side, and for the 3rd one i use simple one-to-many:
here's the code:
class Drummer < ActiveRecord::Base
has_and_belongs_to_many :videos
end
class Cymbal < ActiveRecord::Base
has_and_belongs_to_many :videos
end
class Video < ActiveRecord::Base
has_and_belongs_to_many :drummers
has_and_belongs_to_many :cymbals
belongs_to :event
end
class Event < ActiveRecord::Base
has_many :videos
end
But I think the polymorphic is the better solutions, video should apply two many other models, but I don't know how to make a many-to-many polymorphic association, i already ask a question about this
What about this:
class Interpreter < ActiveRecord::Base
belongs_to :video
end
class Drummer < Interpreter
end
class Cymbal < Interpreter
end
class Video < ActiveRecord::Base
has_and_belongs_to_many :interpreters
has_and_belongs_to_many :drummers
has_and_belongs_to_many :cymbals
belongs_to :event
end
class Event < ActiveRecord::Base
has_many :videos
end
Video.first.interpreters should return all drumers and cymbals, while Video.first.drummers and Video.first.cymbals will return only corresponding models
Cymbals and Drummers will share same database table interpreters
Have you watched this http://railscasts.com/episodes/154-polymorphic-association