Benjamin
Benjamin

Reputation: 2118

Active Admin Gem Model Association.

I just started using Active Admin Gem. I have a little bit of a problem and i was wondering if anyone had a solution.

I have two models

Dogs
Breeders

Using my application i would like to be able to

  1. find dogs breeds and then while viewing the dog have a list of breeders.
  2. When i visit the breeders (show page) i want to be able to see the dog breeds they have.

I have created the models but i am not sure how to accoumplish the task, However u have attempeted by creating a join table

breeders_dogs join table

class BreedersDogs < ActiveRecord::Migration
   def self.up
   create_table 'breeders_dogs', :id => false do |t|
    t.integer :breeder_id
    t.integer :dog_id
  end
  end


   def self.down
  drop_table :breeders_dogs
  end

end

Dogs Model

class Dog < ActiveRecord::Base
   has_and_belongs_to_many :breeders, :join_table => :breeders_dogs

end

Breeder Model

class Breeder < ActiveRecord::Base
   has_and_belongs_to_many :dogs,  :join_table => :breeders_dogs
end

I tried this but the situation is not the same. Using Rails Gem Active Admin with Associations

I am strugling on how to create this association between the two models. Thank you in advance.

Upvotes: 2

Views: 1282

Answers (1)

Ammar
Ammar

Reputation: 1101

# app/admin/dogs.rb


    show :title => [whatever field you want your title to be e.g. :dog_name] do

        panel "Dog Info" do
          attributes_table_for dog do
                row("..label..") { dog.fieldname }
                ...
              end
        end

        panel "List of Breeders" do
            table_for dog.breeders do |t|
                t.column("Breeder List") { |breeder| breeder.name }
            end
        end
    end


# Form Fields

form do |f|

    f.inputs "Dog Info" do
      f.input :inputname
      ...
    end

    f.inputs "Breeders" do
      f.input :breeders
    end
    f.buttons
end

I think this is what you'll need. Remember to adjust the field names etc for your setup. :)

Upvotes: 4

Related Questions