Sa S. wrote in post #1074685:
> Hi Michael,
>
> Thanks for the reply.
>
> I actually have created the join table as documented in the rails
> guides, with the right naming convention and all. It's got a model_1_id
> and a model_2_id, and an index
>
> Sure, I can give some more details. I'm creating a golf application that
> let's users enter in some information about their golf game, and then
> shows them appropriate golf courses.
>
> So I've got one model called 'Course' that is just a bunch of seed data.
> Then I've got another model called 'Player' that contains all the
> user-entered information via a form.
>
> Ultimately, I have a Player 'show' view that shows a table with the
> appropriate golf courses, along with some other information. One of
> these other pieces of information is a mathematical function that
> compares the user's entered handicap with the course's.
>
> So all of my methods are in the Player model, and for this handicap
> function, I'm trying to pull in the Course's handicap attribute to do
> the math.

Rails provides methods for accessing associations between models. You 
should not need to use foreign keys directly as shown in your original 
post.

Given:
1. Player has many courses
2. Course has many players

Example:
a_player = Player.first
players_courses = a_player.courses
player_courses.each do |course|
  puts course.handicap
end

class Player
  def recommended_courses
    Course.where(courses[:handicap].gt(a_player.handicap))
      .and(courses[:handicap].lt(a_player.handicap)
  end
end

These examples are for demonstration only. They are completely untested 
or checked for syntax.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to