Ruby on Rails建议用户的输入和mySQL

时间:2021-12-31 13:00:38

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.

我正在尝试开发一个小应用程序,它将在MySQL数据库中存储用户关于视频的输入。问题是我希望用户为每个添加的视频都有一个mySQL表。

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 ...

示例:Web应用程序将收集用户56的视频输入23并执行mySQL查询“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?

我不知道我的方法是否正确。我首先想到的只是使用文本文件,并将它们存储在服务器上,放在一个单独的文件夹或每个用户中,而不是使用MySQL。有帮助吗?

3 个解决方案

#1


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:

您的表格可以区分通过用户ID列上传每个视频的人,即:

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

然后你可以代替将table_100用于用户的视频,只需在查询中使用WHERE user_id = 100并且只获取该用户的视频

#2


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?

你的方法似乎错了。数据库通常用于保存具有相同特征的许多记录。所以你应该有两个表,一个用于用户,一个用于视频。为什么每个视频都需要一张桌子?

#3


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.

这样,您就可以在视频中获得用户的所有视频评论,并在用户页面上显示所有用户评论,并上传用户视频。这适用于上面的示例,为您提供了显示数据的灵活性。

#1


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:

您的表格可以区分通过用户ID列上传每个视频的人,即:

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

然后你可以代替将table_100用于用户的视频,只需在查询中使用WHERE user_id = 100并且只获取该用户的视频

#2


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?

你的方法似乎错了。数据库通常用于保存具有相同特征的许多记录。所以你应该有两个表,一个用于用户,一个用于视频。为什么每个视频都需要一张桌子?

#3


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.

这样,您就可以在视频中获得用户的所有视频评论,并在用户页面上显示所有用户评论,并上传用户视频。这适用于上面的示例,为您提供了显示数据的灵活性。