Skip to content

Instantly share code, notes, and snippets.

@boyvanamstel
Created May 23, 2011 13:15
Show Gist options
  • Save boyvanamstel/986670 to your computer and use it in GitHub Desktop.
Save boyvanamstel/986670 to your computer and use it in GitHub Desktop.
Multiple relations to the same model in Rails
# Model User:
has_many :questions
has_many :comments
# Questions to which the user is an expert a.k.a. 'questions to me'
has_many :experts, :dependent => :destroy
has_many :questionstome, :through => :experts, :source => :question
# This allows you to do things like this:
# u = User.first
# u.questions.all # lists all questions asked by the user
# u.questionstome.all # lists all questions to which someone made me an expert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment