Ask A Question

Notifications

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.

site#show

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?

schema.rb

case_studies
  title
  content
  position
sites
  address
case_studies_sites
  case_study_id
  site_id
  position
Reply

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) }
end

And then your other models can reference this:

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

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.

Reply

Thanks Chris, this really help!

Reply
Join the discussion
Create an account Log in

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

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

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