Using Rails Gem Active Admin with Associations
Solution 1
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
Solution 2
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).
Solution 3
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.
Related videos on Youtube
Tyler
Updated on June 04, 2022Comments
-
Tyler almost 2 years
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!
-
Tyler almost 13 yearsThanks for the reply - did you get this from some documentation I missed? If so - where is it? The problem is if I do as you show, I would get a column that says "Ingredient". I don't know about you - but that's not quite what I'm looking for. Can I specify a column of the association to display?
-
Rachid Al Maach almost 13 yearsHi Tyler, I got it from the simple form gem documentation, it seems that they are using this. And I also think I misunderstood your question. I haven't got the chance to do what you are trying but I myself would define a virtual attribute for the "Ingredient" or override the name attribute ending up doing something like this:
def self.name "Ingredient #{self.name}" end
I didn't tried it myself so can't promise if it will work for you -
6twenty almost 13 yearsActiveAdmin uses Formtastic (github.com/justinfrench/formtastic), not SimpleForm. Within the
form do
block, it's pretty much all Formtastic doing the work. It's likely that Formtastic also handles the associations, so I'd recommend checking out their documentation for some pointers :) -
Victor Lam over 12 yearsi tried this method. but once i add "f.has_many" block, there is an error "undefined method `new_record?' for nil:NilClass". Do you have any suggestion?
-
Mik over 11 yearsYou need to add
accepts_nested_attributes_for
in model, then it should work.