简体   繁体   中英

How to define a database association

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

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'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM