Search code examples
ruby-on-railsrubysearcharea

Search restaurants by delivery area in a rails app


I want to search restaurants by delivey_area in a ruby on rails app I created a model for area, i made a relationship between area and restaurants by area_id.. What should i add in my code? Currently i can just set one area for each restaurant.. but what i want to do is a restaurant must have more than one delivery area...

This is my restaurant form: (I tried to list all areas with chexboxes to allow restaurant choose delivery areas )

<div class="field">
  <%= f.label :delivery_areas %>
    <% Area.all.each do |area| %>
      <%= check_box_tag("area", "name") %>
      <%= area.name %>
    <% end %>
 </div>

This is my index action in restaurant controller:

def index
  @restaurants = Restaurant.all.order("created_at DESC")
  if params[:area].blank? 
    @restaurants = Restaurant.all.order("created_at DESC")
  else 
    @area_id = Area.find_by(name: params[:area]).id 
    @restaurants = Restaurant.where(:area_id => @area_id).order("created_at DESC")
  end 
end

This is a search form by links:

    <ul>
      <li class="dropdown area-dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" style="width: 440px;">
         area
          <span class="caret"></span>
        </button>

        <ul class="dropdown-menu scrollable-menu" aria-labelledby="dropdownMenu1" style="width: 440px;">
          <% @areas.each do |area| %>
            <li>
              <%= link_to area.name, restaurants_path(area: area.name), class: "link" %>
            </li>
          <% end %>
        </ul>
      </li>
    </ul>

This my restaurant model:

class Restaurant < ApplicationRecord
 belongs_to :area
end

This is my area model:

class Area < ApplicationRecord
 has_many :restaurants
end

Solution

  • this should work if "area" is your remote key column in Restaurant

    Restaurant.find_by_area(Area.find_by_name(params[:area]))
    

    Or if you have "area_id" column in your Restaurant model then:

    Restaurant.find_by_area_id(Area.find_by_name(params[:area]).id)
    

    A bit more info in your question could help get a better answer...


    Update:

    @i5okie Thanks for your reply.. With my code searching by area_id is working but i can only set just one delivery area for each restaurant.. I want a restaurant to have many delivery areas.

    Reply follows:

    Create a Many-to-Many Relationship:

    This will give you a checkbox for every Area.all, checked if they are selected in the Restaurant.area_ids, and unchecked otherwise.

    ## restaurant.rb
    class Restaurant < ApplicationRecord
     has_and_belongs_to_many :areas
    end
    
    ## area.rb
    class Area < ApplicationRecord
     has_and_belongs_to_many :restaurants
    end 
    
    ## migration (for rails 5.0)
    class CreateAreasRestaurants < ActiveRecord::Migration[5.0]
      def change
        create_table :areas_restaurants, id: false do |t|
          t.belongs_to :area, index: true
          t.belongs_to :restaurant, index: true
        end
      end
    end
    
    # in view:
    <div class="field">
      <%= f.label :delivery_areas %>
      <%= f.collection_check_boxes :area_ids, Area.all, :id, :name do |b| %>
        <div class="collection-check-box">
          <%= b.check_box %>
          <%= b.label %>
        </div>
      <% end %>
    </div>
    

    Reading materials:

    How to Save Multiple Checkbox Values to a Database in Rails

    Master Many-to-Many Associations with ActiveRecord