0

I'm working with two tables Video and Picture and I would like to regroup them using SQL instead of ruby. This is how I do it now :

@medias = (Video.all + Picture.all).sort_by { |model| model.created_at }

Is their a way to do the same thing only with SQL/ActiveRecord?

3
  • Do the Video and Picture models have the same number of columns and then same column names? Commented Mar 30, 2017 at 22:51
  • No, picture contains more columns and their name isn't the same Commented Mar 30, 2017 at 22:59
  • My thought was to use SQL UNION but that requires at least the number of columns to be the same. Commented Mar 30, 2017 at 23:26

3 Answers 3

2

Since you don’t have the same columns in each model you could create a polymorphic relationship with a new model called media. Your Videos and Pictures would be associated with this new model and when you need to work on only your media you don’t need to worry about whether it is a video or a picture. I’m not sure if this fits into your schema and design since there is not much info to go on from your post but this might work if you wanted to take the time to restructure your schema. This would allow you to use the query interface to access media. See the Rails Guide here:

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

You can create a media model with all the fields need to satisfy a Video or Picture object. The media model will also have a type field to keep track of what kind of media it is: Video or Picture.

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

Comments

1

Yes, using ActiveRecord's #order:

@video = Video.order(:created_at)
@pictures = Picture.order(:created_at)
@medias = @video.all + @pictures.all # Really bad idea!

Also calling all on the models like that will unnecessarily load them to memory. If you don't absolutely need all records at that time, then don't use all.

3 Comments

I know this is a bad idea that's why i'm trying to do this using only SQL but since their is no relationship between those tables, I can't figure out how to do this
If there is no relation between the tables, you can just paginate each one and create the array with the paginated entries.
This will load Videos ordered by created_at and then Pictures ordered by created_at and then append Pictures to Videos. In the original OP code above the sort happens after the Videos and Pictures are combined so they are intermixed.
0

To run sql queries in Rails you could do this:

sql_statement = "Select * from ..."
@data = ActiveRecord::Base.connection.execute(sql_statement)

Then in your view you could simply reference the @data object

1 Comment

"ActiveRecord::Base.connection" is deprecated: apidock.com/rails/ActiveRecord/Base/connection

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.