ruby on rails - Association between Category, Subcategory, and Lawyer -


i have vast list of lawyers, categories, , subcategories.

hint (so have clue if associations okay)

  1. on categories table, not want see column on categories table referencing subcategories.
  2. on subcategories table, not want see column on subcategories table referencing categories.
  3. not categories has subcategories. i.e. don't have subcategories seen in picture.
  4. i have 2 separate forms creating category , subcategory.
  5. i added category_id , subcategory_id foreign keys lawyers table. such can choose lawyers form upon create, category or subcategory lawyer belong in image.
  6. also note: subcategory created @ time, day, categories not having subcategory, new subcategories under categories having subcategories, , lawyers placed under them.
  7. the image replica of index/homepage having @ moment, @ least before number 6 above takes effect time day, , hope use loop make view happen.

pictorial understanding of trying do:

image description of homepage

here relationships between 3 models

class lawyer < activerecord::base   belongs_to :category   belongs_to :subcategory end  class category < activerecord::base   has_many :lawyers end  class subcategory < activerecord::base   #belongs_to :category #do want "category_id" in subcategories table?   has_many :lawyers end 

question

is association on 3 models okay hint gave? pretty confusing.

you don't need subcategory model/table, specially if have same columns. categories table should have parent_id column. category subcategory when has parent_id value pointing category record. categories null parent_id top level categories.

example

class lawyer < activerecord::base   belongs_to :category end  class category < activerecord::base   has_many :lawyers    # called self referential relation. records in    # table may point other records in same table.   has_many :sub_categories, class_name: "category", foreign_key: :parent_id    # scope load top level categories , eager-load    # lawyers, subcategories, , subcategories' lawyers too.   scope :top_level, -> { where(parent_id: nil).include :lawyers, sub_categories: :lawyers } end 

note: should create migration add parent_id column categories table. , can drop subcategories table.

now create of categories (i'm assuming there's name column):

cat = category.create name: "corporate , commercial law" subcat = category.new name: "corporate tax", parent_id: cat.id subcat.lawyers << lawyer.find_by_name("sabina mexis") subcat.save 

example table of contents:

<% category.top_level.each |cat| %>   <%= cat.name %>   <% cat.sub_categories.each |subcat| %>     <%= subcat.name %>     <%= subcat.lawyers.each |laywer| %>        <%= lawyer.name %>     <% end %>   <% end %> <% end %> 

the above simplified example. hope helps.

update

to enhance form allow create subcategory , assign parent category, use select menu filled top_level category ids:

<%= form_for category.new |f| %>   <%= f.text_field :name %>   <%= f.select :parent_id, options_from_collection_for_select(category.top_level, :id, :name) %>   <%= f.submit %> <% end %> 

check out docs options_from_collection_for_select if it's unfamiliar. build select menu category:id values , :name text in menu. make sure add :parent_id strong parameters allow mass assignment via params[:category].

the laywer error typo in example code, it's fixed now.


Comments