0

I am trying to develop a small app, that will store User's input about Videos in an mySQL Database. The problem is that I want Users to have an mySQL Table for each video added.

so my approach is kinda this one

class User < ActiveRecord::Base

  has_many :Videos

end

Example: The web app will gather User's 56 Input for Video 23 and execute an mySQL query "INSERT INTO video_23_info VALUES ...

Each time I add a new video to the database I want a table to be associated with the User.

I don't know if my approach is correct. I first thought of just using text files, and storing them on the server, into a separate folder or each User, instead of using MySQL. Any help?

3 Answers 3

1

Having a different table for each user would be wrong and a bad practice.

You could instead create a table videos and keep all videos there:

Your table could distinguish who uploaded each video through a user id column ie:

video_id  | user_id | other info
--------------------------------
    1        100         
    2        200         
    3        100         

So you can then query the table to either select all videos, select a particular users videos alone or anything your imagination can come up with.

Then you can instead of use table_100 for a user's videos, just use WHERE user_id = 100 in your query and only get that user's videos

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

1 Comment

Tipote nase kala. O allos eklepse mou kai ton pinaka kai edokes tou tin sosti apantisi? :P
1

Your Approach seems wrong. Databases are usually used to save many records with the same characteristics. So you should have two tables, one for the Users and one for the Videos. Why would you need a table for each Video?

Comments

1

So the question confused me a little bit. I've gather that you want users input about the videos (meaning each video can have multiple inputs from different users?). If this is the case then the approach is not very effective. You can still have the video table with the video information (including who uploaded it if you want that) and the user table with the user's data. Then you can have a third table to store all of the inputs from different users.

    video_id | user_id | comment
      1           1        loved it
      1           2        user2 input
      2           1        user1 input

 class User < ActiveRecord::Base
    has_many :comments
    has_many :videos
 end

 class Video < ActiveRecord::Base
    has_many :comments
    belongs_to :user
 end

 class Comment < ActiveRecord::Base
    belongs_to :user, :video
 end

This way you can have all the video comments from users on the video as well as having all the users comments on the users page and that users videos uploaded. This works with your example from above and gives you a lot of flexibility displaying the data.

2 Comments

I don't really care about who uploaded it. This is an easy approach thanks. So User, Video, and Comment should all be models right?
Yep, they should all be models.If you don't care who uploaded it you can take out the relationship between users and videos.

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.