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
Reply
Join the discussion
Create an account Log in

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

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

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