Rails: Simple form - multilevel collection grouped_select - ruby-on-rails

I have the following associations setup:
class Category < ApplicationRecord
has_many :child_categories
end
class ChildCategory < ApplicationRecord
belongs_to :category
has_many :subcategories
end
class Subcategory < ApplicationRecord
belongs_to :child_category
has_many :child_subcategories
end
class ChildSubcategory < ApplicationRecord
belongs_to :subcategory
end
An example of the above structure is: Apparel(category) - Clothing(child category) - Men(subcategory) - Tshirts(child subcategory).
I have a simple form where I create a product and I would like to associate that product with a child subcategory from a collection grouped_select input. Basically this input will be multileveled, for example: Clothing(cant select this) under that Men(cant select this) and after that Tshirts(I will be able to select this and associate a product with a child subcategory).
I'm kind of stuck on how to populate the collection grouped_select input, I can only get it to show the child categories and subcategories with the following. Any ideas how I can show the child subcategories as well?
#categories = ChildCategory.where(id: params[:category])
<%= f.input :category_id, collection: #categories.order(:name), as: :grouped_select, group_method: :subcategories, include_blank: false, include_hidden: false %>

Related

How to implement a many-to-many in ActiveAdmin?

There a many-to-many:
class Employee < ActiveRecord::Base
has_many :employees_and_positions
has_many :employees_positions, through: :employees_and_positions
end
class EmployeesAndPosition < ActiveRecord::Base
belongs_to :employee
belongs_to :employees_position
end
class EmployeesPosition < ActiveRecord::Base
has_many :employees_and_positions
has_many :employees, through: :employees_and_positions
end
How to implement a choice (check_boxes) positions in the form when adding an employee?
I wrote this variant:
f.inputs 'Communications' do
f.input :employees_positions, as: :check_boxes
end
It displays a list of positions in the form, but does not save nothing to the table (employees_and_positions).
How to fix?
Suppose you have an employee, you can reference the ids of the employees_positions association by using employee.employees_position_ids. Accordingly, you can mass assign pre-existing EmployeesPosition objects using a check_box for each EmployeesPosition, but you need to use the employee_position_ids attribute"
= f.input :employee_position_ids, as: :check_boxes, collection: EmployeesPosition.all
Also, make sure you've whitelisted the employee_position_ids param in your active admin resource:
ActiveAdmin.register Employee do
permit_params employee_position_ids: []
end
http://activeadmin.info/docs/2-resource-customization.html

Rails Nested Json (Jbuilder)

I have these models:
- Category
- SubCategory
- SubSubCategory
I'm sending a category_list.json.jbuilder with the tree and have categories-->subcategories-->subsubcategories.
Thing is that the "subsubcategy" is showing inside the "subcategory" even if don't has. For example:
I just have one "subsubcategory1" that belongs to "subcatgory1" but appears in "subcategory1" are showing "subsubcategory1, subsubcategor2, etc".
Category model:
has_many :sub_categories
has_many :sub_sub_categories
SubCategory model.
belongs_to :category
belongs_to :sub_category
belongs_to :category
has_many :sub_sub_category, through: :categories
SubSubCategory model:
This in my code:
json.categories #categories do |category|
json.name category.name
json.description category.description
json.category_id category.id
json.sub_categories category.sub_categories do |subcategory|
json.name subcategory.name
json.description subcategory.description
json.sub_category_id subcategory.id
json.sub_sub_categories category.sub_sub_categories do |subsubcategory|
json.name subsubcategory.name
json.description subsubcategory.description
json.sub_sub_category_id subsubcategory.id
end
end
end
I just have one "subsubcategory1" that belongs to "subcatgory1" but appears in "subcategory1" are showing "subsubcategory1, subsubcategor2, etc".
According to your model relations code, your SubSubCategories belong to Categories, and not SubCategories. So, it means that if a category has a subsubcategory, all categories within that category will have that subsubcategory.
I think you want something like this:
class Category < ActiveRecord::Base
has_many :sub_categories
has_many :sub_sub_categories, through: :sub_categories
end
class SubCategory < ActiveRecord::Base
belongs_to :category
has_many :sub_sub_categories
end
class SubSubCategory < ActiveRecord::Base
belongs_to :sub_category
end

Polymorphic associations and Formtastic in ActiveAdmin

I have a simple polymorphic association:
class Highlight < ActiveRecord::Base
belongs_to :highlightable, polymorphic: true
end
class Property < ActiveRecord::Base
has_many :highlights, as: :highlightable
end
class Destination < ActiveRecord::Base
has_many :highlights, as: :highlightable
end
In the active_admin form to create a new Highlight how can i assign it to either a Property or a Destination?
You can add a this to the highlight form:
f.input : highlightable_type, as: :select, collection: {"Property" => "property", "Destination" => "destination"}

collection_select with has_many :through relationship table data

I have such terrible models:
class ParentalRelation < ActiveRecord::Base
belongs_to :parent
belongs_to :student
belongs_to :counselor
belongs_to :parental_relation_type
end
class ParentalRelationType < ActiveRecord::Base
has_many :parental_relations
end
class Parent < ActiveRecord::Base
has_many :parental_relations
has_many :students, :through => :parental_relations
has_many :counselors, :through=> :parental_relations
has_many :parental_relation_types, :through=> :parental_relations
belongs_to :user, :dependent=> :destroy
belongs_to :occupation_type
accepts_nested_attributes_for :user
end
Parental relation types are like father, mother, etc. The reasoning is that a parental relation between one counselor, one parent and one student is unique and counselors should not see the relations that belong other counselors.
In controllers/parent_controller/edit action I have:
#parental_relation= ParentalRelation.find_by_counselor_id_and_student_id_and_parent_id(x, y, z)
In views/parent/_form.html.erb I have:
<%= form_for #parent do |f| %>
inside that form I need a collection_select for ParentalRelationType.all and select the parent's parental_relation_type_id for that particular parental relation, but I can't find the syntax to do it.
I tried adding
<%= collection_select(#parental_relation, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
underneath the form_for, but the relation type id is 2, and default 1 is selected instead.
Added this to parents/_form
<%= fields_for #counselor_student_parent do |csp| %>
<%= f.label :parental_relation_type_id %>
<%= collection_select(:student_counselor_parent, :parental_relation_type_id, ParentalRelationType.all, :id, :name) %>
<% end %>
And this to parents_controller/new
def new
#counselor= Counselor.find(params[:counselor_id])
#student= Student.find(params[:student_id])
#parent= #student.parents.build
#parent_user= #parent.build_user
#counselor_student_parent= #counselor.student_counselor_parents.build
end

category structure

I am having trouble with some theory.
I have a model called Promos, and I also have a model called Categories.
I want the admin to be able to create set Categories from which the users will select in a dropdown to assign the Promo. So Promos will belong to a Category but the assignment ought to happen in the create.
What is the recommended structure?
To ensure that every Promo has a Category:
class Category < ActiveRecord::Base
has_many :promos
end
class Promo < ActiveRecord::Base
belongs_to :category
validates_association_of :category
end
How to set the Category at Promo creation time
promo = Promo.new(:category => #category)
As far as forms go:
<% form_for :promo do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => "Choose a category" %>
...
Other promo fields
...
<% end %>
Matching controller code:
class PromosController < ActionController
def create
#promo = Promo.create(params[:promo])
...
redirect or render whether #promo was successfully created
...
end
end
A user has_many a promos, which belongs to a category.
A category has_many promos.
Such as:
class User < Activerecord::Base
has_many :promos
class Promo < Activerecord::Base
belongs_to :user
belongs_to :category
class Category < Activerecord::Base
has_many :promos

Resources