如何定义数据库关联

发布于 2024-12-26 03:07:54 字数 619 浏览 1 评论 0原文

我目前正在开发一个在线应用程序,使我们学校的体育学院能够存储他们的比赛表现并对其进行排名。我正在尝试合并来自 2 个独立数据库的信息 - ResultsAthletes

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北凤男飞 2025-01-02 03:07:54

我建议使用 has_many 关联来执行此操作。它将稍微简化您的生活,并删除 Result 模型中不必要的 :name 属性。

class Athlete < ActiveRecord::Base
  has_many :results

  # == Schema Information
  #
  # Table name: athletes
  #
  #  id         :integer         not null, primary key
  #  name       :string(255)
  #  gender     :string(255)
  #  age        :integer
  #  created_at :datetime
  #  updated_at :datetime
end

class Result < ActiveRecord::Base      
  belongs_to :athlete

  # == Schema Information
  #
  # Table name: results
  #
  #  id           :integer         not null, primary key
  #  athlete_id   :integer
  #  event        :string(255)
  #  performance  :integer
  #  created_at   :datetime
  #  updated_at   :datetime
end

然后你的搜索将是:

@results_by_age = Result.joins(:athlete).where(:athletes => { :name => params[:name], :age => params[:age] })
@results_by_event = Result.where(:event => params[:event]).joins(:athlete).where(:athletes => { :name => params[:name] })
@results_by_gender = Result.joins(:athlete).where(:athletes => { :name => params[:name], :gender => params[:gender] })

结果 SQL 将是:

# Filtered by name and age
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"athletes\".\"name\" = 'Bob' AND \"athletes\".\"age\" = 25"

# Filtered by event and name
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"results\".\"event\" = 'Something' AND \"athletes\".\"name\" = 'Bob'"

# Filtered by name and gender 
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"athletes\".\"name\" = 'Bob' AND \"athletes\".\"gender\" = 'Confused'"

I would recommend doing this using a has_many association. It will simplify your life a little and remove the unnecessary :name attribute in the Result model.

class Athlete < ActiveRecord::Base
  has_many :results

  # == Schema Information
  #
  # Table name: athletes
  #
  #  id         :integer         not null, primary key
  #  name       :string(255)
  #  gender     :string(255)
  #  age        :integer
  #  created_at :datetime
  #  updated_at :datetime
end

class Result < ActiveRecord::Base      
  belongs_to :athlete

  # == Schema Information
  #
  # Table name: results
  #
  #  id           :integer         not null, primary key
  #  athlete_id   :integer
  #  event        :string(255)
  #  performance  :integer
  #  created_at   :datetime
  #  updated_at   :datetime
end

And then your searches would be:

@results_by_age = Result.joins(:athlete).where(:athletes => { :name => params[:name], :age => params[:age] })
@results_by_event = Result.where(:event => params[:event]).joins(:athlete).where(:athletes => { :name => params[:name] })
@results_by_gender = Result.joins(:athlete).where(:athletes => { :name => params[:name], :gender => params[:gender] })

The resulting SQL would be:

# Filtered by name and age
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"athletes\".\"name\" = 'Bob' AND \"athletes\".\"age\" = 25"

# Filtered by event and name
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"results\".\"event\" = 'Something' AND \"athletes\".\"name\" = 'Bob'"

# Filtered by name and gender 
"SELECT \"results\".* FROM \"results\" INNER JOIN \"athletes\" ON \"athletes\".\"id\" = \"results\".\"athlete_id\" WHERE \"athletes\".\"name\" = 'Bob' AND \"athletes\".\"gender\" = 'Confused'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文