DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

A Little Helper To Help You :through

Jack Hung user avatar by
Jack Hung
·
Aug. 27, 06 · · Code Snippet
Like (0)
Save
Tweet
433 Views

Join the DZone community and get the full member experience.

Join For Free
Do you have to code a lot of "has_many :through" relation models in your Rails application? Here is a little helper that will help you DRY. It is very simple indeed, for an example here are the models:
Party, ContectMech and PartyContectMech(the join model).

Call many_to_many_through, perhaps in your application.rb

# many_to_many_through PartyContectMech, Party, ContectMech returns:
#    Party.has_many :party_contact_meches, :foreign_key => :party_id
#    Party.has_many :contact_meches, :through => :party_contact_meches, :foreign_key => :party_id
#
#    ContactMech.has_many :party_contact_meches, :foreign_key => :contact_mech_id
#    ContactMech.has_many :parties, :through => :party_contact_meches, :foreign_key => :contact_mech_id
#
#    PartyContactMech.belongs_to :party
#    PartyContactMech.belongs_to :contact_mech
#
# evaluating the result defines the necessary 6 relations,
# * save some typing, 
# * no need to jump between the 3 source files,
# * less chance for typo and/or human mistake
  eval many_to_many_through(PartyContectMech, Party, ContectMech)



SOURCE:

  def many_to_many_through(through_model, model1, model2)
    through_table = through_model.table_name
    table1 = model1.table_name
    table2 = model2.table_name
    model1_id = model1.name.underscore + "_id"
    model2_id = model2.name.underscore + "_id"
%Q{
    #{model1}.has_many :#{through_table}, :foreign_key => :#{model1_id}
    #{model1}.has_many :#{table2}, :through => :#{through_table}, :foreign_key => :#{model1_id}

    #{model2}.has_many :#{through_table}, :foreign_key => :#{model2_id}
    #{model2}.has_many :#{table1}, :through => :#{through_table}, :foreign_key => :#{model2_id}

    #{through_model}.belongs_to :#{model1.name.underscore}
    #{through_model}.belongs_to :#{model2.name.underscore}
}
  end

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • Making Machine Learning More Accessible for Application Developers
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry
  • Exporting and Importing Projects in Eclipse

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo