Created
March 16, 2018 21:41
-
-
Save stungeye/62b6c6411302aa18c6c0fe8fb07dd33a to your computer and use it in GitHub Desktop.
Has Many Through With ActiveAdmin on Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/board_game.rb | |
class BoardGame < ApplicationRecord | |
has_many :board_game_categories | |
has_many :categories, through: :board_game_categories | |
## This line was added: | |
accepts_nested_attributes_for :board_game_categories, allow_destroy: true | |
has_many :board_game_mechanics | |
has_many :mechanics, through: :board_game_mechanics | |
has_many :board_game_designers | |
has_many :designers, through: :board_game_designers | |
validates :name, presence: true, uniqueness: true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/admin/board_games.rb | |
ActiveAdmin.register BoardGame do | |
permit_params :name, board_game_categories_attributes: [:id, :board_game_id, :category_id, :_destroy] | |
index do | |
selectable_column | |
column :id | |
column :name | |
column :rank | |
column :categories do |board_game| | |
board_game.categories.map { |bg| bg.name }.join(", ").html_safe | |
end | |
actions | |
end | |
show do |board_game| | |
attributes_table do | |
row :name | |
row :rank | |
row :categories do |board_game| | |
board_game.categories.map { |bg| bg.name }.join(", ").html_safe | |
end | |
end | |
end | |
form do |f| | |
f.semantic_errors *f.object.errors.keys | |
f.inputs "Board Game" do | |
f.input :name | |
f.input :rank | |
f.has_many :board_game_categories, allow_destroy: true do |n_f| | |
n_f.input :category | |
end | |
end | |
f.actions | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment