2

I can edit a join table in ActiveAdmin, only if the join table has one, and only one, primary key (id).

If I try to design a join table with only two fields (primary key on both), ActiveAdmin returns a NilClass error when I attempt to add a new record : undefined method 'humanize' for nil:NilClass.

model :

class IngredientProduct < ActiveRecord::Base
  belongs_to :product
  belongs_to :ingredient
end

class Ingredient < ActiveRecord::Base
end

class Product < ActiveRecord::Base
end

ingredient_products fields :

product_id --> primary key, nn, integer
ingredient_id --> primary key, nn, integer

That doesn't work. My table "ingredient_products" must be designed this way :

id --> primary key, nn, integer
product_id --> integer
ingredient_id --> integer

to allow me to edit or add record with ActiveAdmin.

Do you know if ActiveAdmin support join tables that have only two fields, both used to define the primary key ?

My project : MySql, Rails 4.0.1 with ActiveRecord and ActiveAdmin 1.0.0.pre

Eric Lavoie
  • 5,121
  • 3
  • 32
  • 49
  • Really I don't think that can be a good idea to add rows to your IngredientProduct table manually. Maybe you can have a good reason, but I think that should be avoided as much as possible. – hmartinezd Feb 15 '14 at 04:53
  • I'm not an expert with ActiveAdmin, you suggest that the association between Ingredients and Products should be done in Products or Ingredients form ? (If so, how you do that?) – Eric Lavoie Feb 15 '14 at 14:18
  • 1
    That's what I meant. So you have a Products form, and an Ingredients form, you can add to each one a non-required field for the other Id, and then override the create method for each one, and that pretty much all. Check here: http://stackoverflow.com/questions/13675765/rails-activeadmin-overriding-create-action and here http://stackoverflow.com/questions/10524012/insert-a-non-input-row-into-a-formtasic-form – hmartinezd Feb 15 '14 at 14:38

2 Answers2

2

The ID is actually the the Primary Key in this situation, whereas product_id and ingredient_id are foreign keys, as they are they keys that reference the primary key of another table. A Primary Key is important because there should only be one. However, you can have the combination of your two foreign keys be unique, by adding database constraints.

in a migration, do this:

add_index :ingredient_products, [:product_id, :ingredient_id], unique: true

If you are intent on using a composite primary key, this gem will do the trick I believe.

https://github.com/composite-primary-keys/composite_primary_keys

Sean
  • 983
  • 5
  • 13
  • As far as I know you can have more than one column to "make" your primary key : a primary key is a combination of columns which uniquely specify a row. I'm sure your solution is fine (thanks), but changing the table structure is what I'm trying to avoid. – Eric Lavoie Feb 15 '14 at 00:52
  • You may have a composite primary key(I think), but I don't believe it is especially good for performance. The index for all intents and purposes is the same (since the combination is unique, as a primary key must be), and doesn't change the table structure at all. It just adds the index to boost performance. May I enquire for what reason's you require a composite primary key? – Sean Feb 15 '14 at 02:40
  • Here is some more info as to what you are looking for. https://github.com/composite-primary-keys/composite_primary_keys. If this is sufficient I'd be much obliged if you could accept the answer. If you need any more information let me know. – Sean Feb 15 '14 at 02:58
2

If you want a join table without an id column, you'll want to omit the model file entirely (IngredientProduct in this case) and use a has_and_belongs_to_many on both ends. ActiveAdmin appears to be able to edit habtm associations:

http://www.mickgardner.com/2012/12/habtm-and-activeadmin-forms.html

Matt Jones
  • 544
  • 3
  • 5