如何定义数据库关联
我目前正在开发一个在线应用程序,使我们学校的体育学院能够存储他们的比赛表现并对其进行排名。我正在尝试合并来自 2 个独立数据库的信息 - Results
和 Athletes
Athletes
包含诸如 :name :gender :age< /代码>。
结果
包含的信息包括:name :event :performance
除了事件之外,我还想按年龄和/或性别过滤结果。
我已经设置了代码来指定要显示的数据的用户首选项,为 :justgender :justage
和 :justevent
设置值
但是,我正在努力过滤结果相应。
看来,最简单的方法恐怕就是在两个模型之间建立“属于”关系,名称可以作为标识符。然而,通常当使用它时,用户会将某个值存储为参数,这可以用于将评论与正确的帖子相关联。
我想设置一种方法,使用户能够输入结果,指定运动员姓名,并用于识别要识别的运动员数据库中的条目以将结果关联起来。我假设一旦完成此操作,我就可以使用 SQL 过滤器来提取匹配结果?
感谢您的帮助
I am currently developing a online application enabling a sports academy at our school to store and rank their competition performances. I am attempting to combine information from 2 separate databases - Results
and Athletes
Athletes
contains information such as :name :gender :age
.Results
contains information including :name :event :performance
I would like to filter the results by age and/or gender in addition to event.
I have set up the code to specify the user preference for the data to show, setting a value for :justgender :justage
and :justevent
However, I am struggling to filter the results accordingly.
It seems that the easiest method is probably to set up a "belongs to" relationship between the two models, the name can be used as the identifier. However, typically when this is used a user would have a certain value stored as a param and this could be used to associate the comment for instance with the correct post.
I would like to set up a method enabling the user to enter a result, specify the athlete name and this be used to identify the entry in the Athletes
database to be identified to associate the result with. I am assuming that once this is done I can just use a SQL filter to pull out the matching results?
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用
has_many
关联来执行此操作。它将稍微简化您的生活,并删除Result
模型中不必要的:name
属性。然后你的搜索将是:
结果 SQL 将是:
I would recommend doing this using a
has_many
association. It will simplify your life a little and remove the unnecessary:name
attribute in theResult
model.And then your searches would be:
The resulting SQL would be: