Skip to content

Instantly share code, notes, and snippets.

@Pavling
Created February 23, 2010 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pavling/312634 to your computer and use it in GitHub Desktop.
Save Pavling/312634 to your computer and use it in GitHub Desktop.
# worker model
belongs_to :project1, :class_name => "Project", :foreign_key => "project1_foreign_key_name_in_workers_table"
belongs_to :project2, :class_name => "Project", :foreign_key => "project2_foreign_key_name_in_workers_table"
belongs_to :project3, :class_name => "Project", :foreign_key => "project3_foreign_key_name_in_workers_table"
# The project model is going to be harder, and you're not going to be
# able to intuitively assign workers to projects, but you can at least
# fudge-together some helper methods to make it look like you're
# leveraging the power of Rails! :-)
#project model
has_many :project1_keyed_workers, :class_name => "Worker", :foreign_key => "project1_foreign_key_name_in_workers_table"
has_many :project2_keyed_workers, :class_name => "Worker", :foreign_key => "project2_foreign_key_name_in_workers_table"
has_many :project3_keyed_workers, :class_name => "Worker", :foreign_key => "project3_foreign_key_name_in_workers_table"
def workers
# build and array of all the arrays of workers, flatten it and get rid of nil values
([] << project1_keyed_workers << project2_keyed_workers << project3_keyed_workers).flatten.compact
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment