0

I have three models:

data_set.rb

class DataSet < ActiveRecord::Base
  has_many :browse_options
  accepts_nested_attributes_for :browse_options, allow_destroy: true 
end

browse_option.rb

class BrowseOption < ActiveRecord::Base
  belongs_to :data_set
  has_many :browse_option_datas
  accepts_nested_attributes_for :browse_option_datas, allow_destroy: true 
end

browse_option_data.rb

class BrowseOptionData < ActiveRecord::Base
  belongs_to :browse_options

  has_one :tradesman
end

I want to be able to display all the tradesman associated with a data set in the data set view with no duplicates. Is there a way I can use the joins method to do this in the controller? Thanks!

1 Answer 1

1

You can actually achieve this by setting up has_many through relationships between your models. There are great docs on this topic.

class DataSet
  has_many :browse_options
  has_many :browse_option_datas, :through => :browse_options
  has_many :tradesmen, :through => :browse_option_datas
end

class BrowseOption
  belongs_to :data_set
  has_many :browse_option_datas
end

class BrowseOptionData
  belongs_to :browse_options
  belongs_to :tradesman
end

class Tradesman
  has_many :browse_options_data
end

Edit: After some discussion in chat we also realised the relationships between Tradesman & BrowseOptionData needed some fixing.

Now, in your controller, you can call:

@data_set = DataSet.first
@tradesmen = @data_set.tradesmen # .uniq if you don't want any duplicates
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.