All threads / How to I access a position attribute on a join model?
Ask A Question


You’re not receiving notifications from this thread.

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

Thomas Bush asked in General

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!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,337+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.