Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

How to I access a position attribute on a join model?

General • Asked by Thomas Bush

I have case studies and site models in my database. I have a has and belongs to many association set up between them.


When on a site#show page I want to write the following erb

<% @site.case_studies.order("position").each do |case_study| %>

The result is the correct case studies, but instead gives the case_studies table position, as opposed to the case_studies_sites table position which I would expect/need. How would I get the joint table's position attribute instead of the Model's position attribute. Does this make sense what I am asking?



Welcome to the gotcha that is has_and_belongs_to_many :) It's not really designed to do this. However! You can just swap the association out with as has_many through and it will work just the same:

# This might not exactly map to the same database table name, so may need to either tweak the name of this model or rename your database table
class CaseStudySite < ActiveRecord::Base
  belongs_to :case_study
  belongs_to :site

  # Here you can just set this up so that it's automatically sorted every time
  default_scope ->{ order(position: :asc) }

And then your other models can reference this:

class Site
  has_many :case_study_sites
  has_many :case_studies, through: :case_study_sites

Do the reverse of this for the CaseStudy model and that'll give you access to everything.

Now that you've got a model representing the join table, you can have full access to it and adjust the positions as you like.

Thanks Chris, this really help!

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.