Codejoy
Codejoy

Reputation: 3806

Getting at data from a join table in rails with known information

EDIT: This was answered but I really meant to ask a more complex question...didn't want to open a new one the answer helped but it wasn't a full answer for what I am trying to do... more edits after code.

I have a join table that joins two models: A group and a Location (which is actually an address id from the address table). This join table is called Group_Locations

class Provider < ActiveRecord::Base
    has_and_belongs_to_many :groups
    belongs_to :designation
    belongs_to :specialty
    has_many :provider_locations
    has_many :invoices
    has_many :groups, through: :provider_locations

end

class ProviderLocation < ActiveRecord::Base
  belongs_to :provider
  belongs_to :group_location

end

class Group < ActiveRecord::Base


    #validations
    validates :group_name, presence: true

    #associations
    has_many :providers 
    has_many :invoices
    has_one  :billing

    has_many :group_locations
end

That is great my other model is:

class GroupLocation < ActiveRecord::Base
  belongs_to :address
  belongs_to :group
  has_many :provider_locations

end

and Address:

class Address < ActiveRecord::Base
    has_many :group_locations
    has_many :billings
end

So with all these associations I can write a nice test:

require 'test_helper'

class GroupLocationTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true
  # end

   test "adding a group and an address" do
    group = Group.new
    group.group_name = 'MyGroup'
    group.save

    adr = Address.new
    adr.city = 'Las Cruces'
    adr.state = 'New Mexico'
    adr.zip = '88012'
    adr.address_line_one = '382 Dark side of moon'
    adr.address_line_two = ''
    adr.save    

    gl  = GroupLocation.new
    gl.group = group
    gl.address=adr      
    assert gl.save, 'Group location did not save'
    puts gl.group.group_name
  end
end

EDIT:::: Thanks Daiku helped, but I have two other models (Provider and ProviderLocation) that now I want to the addresses and group names given the name of a provider.

Their relationship is a join table called ProviderLocation that has a group_location id and a provider_id in it. So I am not sure t he associations (another through I am sure) I need to get to it... I had in my Provider model:

has_many :groups, through: :group_locations

This didn't work gave me an error when trying to use:

puts Provider.where(first_name: "Shane").first.groups.first.group_name
1) Error:
ProviderLocationTest#test_adding_a_provider_to_a_group_location:
NoMethodError: undefined method `klass' for nil:NilClass
    test/models/provider_location_test.rb:37:in `block in <class:ProviderLocationTest>'

Upvotes: 0

Views: 127

Answers (1)

Daiku
Daiku

Reputation: 1227

In your Group model, add an association:

has_many :addresses, through: :group_locations

Then you can:

Group.where(group_name: "MyGroup").first.addresses.first

Upvotes: 1

Related Questions