1

This is my shema :

class Media< ApplicationRecord
  has_many :pictures
  has_many :videos
end
class Picture < ApplicationRecord
  belongs_to :media
end
class Video < ApplicationRecord
  belongs_to :media
end

Is it possible to get all medias that correspond to the model picture?

0

1 Answer 1

2

Look into joins - it will return you all medias which have at least single picture associated:

Media.joins(:pictures)

If you mean to get the specific picture's media, then you just use the method provided by association:

Picture.first.media
Sign up to request clarification or add additional context in comments.

2 Comments

will it work, or will it raise an error/warning or sth like that?
@marmeladze I am not sure. The only possible issue here, is that Rails' Inflector might get lost. But that's easy fixed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.