Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rails Gem Active Admin with Associations

I'm trying out the new Rails gem http://activeadmin.info/ and it's working great! However I can't find any documentation on how to use it across associations. For example:

class Membership < ActiveRecord::Base
  belongs_to :course
  belongs_to :person

class Course < ActiveRecord::Base
  has_many :memberships
  has_many :people,  :through => :memberships

class Person < ActiveRecord::Base
  has_many :memberships
  has_many :courses, :through => :memberships

The membership join table includes some extra data as well (ie: attendance). I'm trying to show the membership with both the course and student name - and allow filtering / sorting on those names. As far as I have found, Active Admin doesn't work across associations. Has anyone else been successful in doing that, or found another gem that does? Thanks so much!

like image 597
Tyler Avatar asked May 16 '11 23:05

Tyler


3 Answers

ingredient.rb

class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :ingredients_products
end

product.rb

class Product < ActiveRecord::Base
  has_and_belongs_to_many :ingredients, :join_table => :ingredients_products
end

don't forget the migrations for the joining table (:id to false!)

class CreateProductsIngredients < ActiveRecord::Migration
  def self.up
    create_table :ingredients_products,:id => false do |t|
      t.integer :product_id
      t.integer :ingredient_id
      t.timestamps
    end
  end

  def self.down
    drop_table :products_ingredients
  end
end

Now define the form in you ActiveAdmin resource, override the default

ActiveAdmin.register Product do
  form do |f|
        f.inputs "Details" do
          f.input :product_name
          f.input :brand
          f.input :ingredients # don't forget this one!
        end
end
like image 101
Rachid Al Maach Avatar answered Nov 05 '22 05:11

Rachid Al Maach


I've been playing with ActiveAdmin for a while now, here's how I managed to get associations to work in Indexes and Forms.

I've just guessed some of your model columns below. Also note, in the form. The 'person' section will show all the columns for editing, whereas the 'course' section will just show the specified column.

ActiveAdmin.register User do
    index do
        column :id
        column :name
        column :attendance
        column :person do |membership|
            membership.person.name
        end
        column :course do |membership|
            membership.course.name
        end
        default_actions
    end

    form do |f|
        f.inputs "Membership" do
            f.input :name
            f.input :created_at
            f.input :updated_at
        end
        f.inputs :name => "Person", :for => :person do |person|
            person.inputs
        end
        f.inputs :name => "Course", :for => :course do |course|
            course.input :name
        end
        f.buttons
    end
end

I haven't tested this, but you should be able to apply these ideas to your case. It's working for mine.

Update: I've just read your question again and noted that you're wanting to be able to sort on the association column. I've just checked my implementation and this indeed is not working. My answer may be useless to you but I'll leave it here anyway (might help someone else).

like image 4
Dave Morro Avatar answered Nov 05 '22 05:11

Dave Morro


I've just started using this gem myself, and while I haven't gotten around to showing association information, here's how you create a form for associations:

form do |f|
  f.inputs

  f.has_many :associations do |association|
     association.inputs
  end

  f.buttons
end

That will give you a basic form with scaffolding.

like image 1
Andres Freyria Avatar answered Nov 05 '22 03:11

Andres Freyria