English 中文(简体)
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!

问题回答

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

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).

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.

ingredient.rb

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

product.rb

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

migration_xxx.rb

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

products.rb

ActiveAdmin.register Product do
  form do |f|
     f.inputs "Details" do
        f.input :product_name
        f.input :brand
        f.input :ingredients
     end
  end
  ...
end




相关问题
Ruby net-dns reverse lookups

I am trying to get reverse DNS lookups working with the net-dns gem for ruby. From the rdoc res = Net::DNS::Resolver.new ip = IPAddr.new("172.16.100.2") packet = res.search(ip) packet = res.search("...

How can I show updates with gem?

I want on my webserver a script that checks automatical over cron if there are gem updates without installing updates. I don t find anything about this task in the gem docs.

missing rake tasks?

I have ran gem install rails and am running 2.3.4 but i am missing some rake tasks like db and gems if i run rake -T i get the following tasks. How can i get all the others ? rake apache2 # ...

Ruby Daemons Gem

I installed the ruby gem Daemons. To make sure it was working I created a script that would print to a file every 5 seconds. I then created another file to run the script using the trivial example ...

Ruby gems repository

I m trying to set a gem repository on one of our local servers. Here are the steps I m running, that I ve followed from several guides. 1) I create the BASEDIR folder /var/www/html/gems 2) sudo cp -...

Search logic functionality

The functionality I m looking for: I have a form that will search my Proposal model. I want the form to contain a select box and display the categories I have. Now, category is merely a column in ...

Ruby Geolocation Gem/Plugins [closed]

What are the available (best) ruby IP-based geolocation gem/plugins? How do they compare to one another in terms of functionality, performance and ease of use (e.g. do they interact with a web ...

How do I find gems that depend on a given gem?

Is it possible to search for all gems that rely on a certain rubygem? For example, I d like to ask for all gems in gemcutter that rely on the test-unit gem. Background: I m looking to see how other ...

热门标签