2

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.

Community
  • 1
  • 1
Benjamin
  • 2,108
  • 2
  • 26
  • 46
  • Just to understand your question: On your Dog show page you need to list associated Breeders and on your Breeder page you need to list associated Dogs? On top of that you, on both show pages, need a way to be able to add new associations? – Thomas Watson Oct 07 '11 at 07:58
  • That's what i'm trying to do. – Benjamin Oct 07 '11 at 08:10

1 Answers1

4
# 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. :)

Ammar
  • 1,091
  • 12
  • 23
  • Am i still keeping the :join_table => :breeders_dogs ? am i adding a new column in the both migrations? I think i have gotten myself confused. – Benjamin Oct 07 '11 at 09:17
  • i don't seem to have a drop down of the breeders, i just have a text field. I want to be able to select the breeder from a drop down list (meaning an association with the two models) – Benjamin Oct 07 '11 at 10:16
  • Hmmm, if the association is built correctly, that should have displayed as a multi-select box. Have you copied the form code for breeders exactly? – Ammar Oct 07 '11 at 10:47
  • Ok, My models are what i have on top. has_and_belongs_to_many :dogs, :join_table => :breeders_dogs. Is this going to save the information in the join table? – Benjamin Oct 07 '11 at 11:59
  • Now works..... but not on a drop down. wow, in a text box. I imagine i will have over 100 breeders.Selecting them would be a challenge. Do you think it's possible to do something like this? http://railscasts.com/episodes/197-nested-model-form-part-2 – Benjamin Oct 07 '11 at 12:12