How to improve this association in Rails - ruby-on-rails

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

Related

Find records based on its other parent child

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)

Select attributes from multiple tables with no association

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.

How do I associate my objects so that a many-to-many relation can be used as one-to-many in Rails?

Please excuse the confusing phrasing in the title. In my RoR project let's say I have it set up like this
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
and
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
I then have a categories_products table that connects them. This works fine but my problem is that a product will only ever have one category at a time and I'd of course like to do product.category instead of having to deal with an array. How can I accomplish that?
A one-to-many representation is demonstrated in the rails guides like this:
class Category < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :category
end

Rails 3: How should I set up this has_many relationship?

Trying to figure out the best way to set up my models. Here's what I've going going on...
Models: Dog, Video, Photo, User
class Dog < ActiveRecord::Base
has_many :videos
has_many :photos
belongs_to :user
end
class Video < ActiveRecord::Base
has_many :dogs
belongs_to :user
end
class Photo < ActiveRecord::Base
has_many :dogs
belongs_to :user
end
class User < ActiveRecord::Base
has_many :dogs
has_many :videos
has_many :photo
end
Should I do has_many :through and a polymorphic?
In my previous iteration of this, I had DogPhoto and DogVideo models, but seems like I could have a single DogItem model that's polymorphic.
Thoughts?
Yes, a polymorphic association would apply here, and it would be a good practice. However, i think that you should have a new model like DogMedia or so, that would be the polymorphic association.
This way a user has a dog and a dog has many DogMedias. A DogMedia is a polymorphic association that can either be a photo, video or anything else you like :)
You could use has many through to get dogmedia for a user's dog directly yes. Something like :
User has many dog_medias through dog (plain language)
or just traverse it through dog :
user.dog.dog_media
If you do the latter, you can even create a Media instead of DogMedia class, create a delegate and execute the neat :
user.dog_media
directly ( The law of demeter : http://en.wikipedia.org/wiki/Law_of_Demeter )

What's the difference between polymorphic and write several one-to-many association

Hey guys,
I'm new to rails
here are the 2 ways of making a Drummer model and Cymbal model both have many videos
1st way by using polymorphic:
class Drummer < ActiveRecord::Base
has_many :videos, :as => :videoable
end
class Cymbal < ActiveRecord::Base
has_many :videos, :as => :videoable
end
class Video < ActiveRecord::Base
belongs_to :videoable, :polymorphic => true
end
2nd way by using two 1:m association:
class Drummer < ActiveRecord::Base
has_many :videos
end
class Cymbal < ActiveRecord::Base
has_many :videos,
end
class Video < ActiveRecord::Base
belongs_to :drummer
belongs_to :cymbal
end
I haven't try them in console, But I think both will work as they should. But I don't know the difference?
I believe that you must use polymorphic method because a model cannot belongs_to (one to one association) more than one other model. For more info see this rails guide: http://guides.rubyonrails.org/association_basics.html
It depends on the columns you have in your database. If you have videoable_type and videoable_id you're doing polymorphism. In that case, calling videoable on an instance of Video can return anything, it's not related to drummers or cymbals. If it is drummer_id and cymbal_id it's the latter version you described.
Both will work.
Imagine you have 5 models sharing Videos.
You would need 5 modelName_id columns in videos.
To determine which type of parent model you have on Video you would need to check each one for id presence.
Validating that only one of the ids is set would be necessary (or valuable).
Polymorphic relations are easier to maintain and expand in such cases.

Resources