All threads / Nested has many through... Or is there a better way?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Nested has many through... Or is there a better way?

Stephen Sizer asked in Rails

Hi I have 2 models Tender and Group shown below.

For a Tender you can add Contractors (contractor_details) and you can also add Groups (groups). Groups also contain contractor_details.

This is so that they may want to categorize certain contractors they use alot into specific groups by speciality e.g. Joiners, Electrians e.t.c..

What would the best way for me to check all contractors added to a tender including through groups, this is so that I can then do authorization and only allow those contractors added to view the tender page?

class Group < ApplicationRecord
  belongs_to :company_detail
  has_many :group_contractors, dependent: :destroy
  has_many :contractor_details, through: :group_contractors
  has_many :tender_groups
  has_many :tenders, through: :tender_groups

  accepts_nested_attributes_for :group_contractors

  validates :name, presence: true
end
class Tender < ApplicationRecord
  belongs_to :user
  belongs_to :company_detail
  has_many :questions
  has_many :assets
  has_many :tender_groups, dependent: :destroy
  has_many :groups, through: :tender_groups
  has_many :tender_contractors, dependent: :destroy
  has_many :contractor_details, through: :tender_contractors
  has_many :bids

  accepts_nested_attributes_for :tender_groups
  accepts_nested_attributes_for :tender_contractors
  accepts_nested_attributes_for :assets

  validates :title, :length_days, :summary, presence: true  

end
Join the discussion

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

Join 33,665+ 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.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.