I create a Rails application for delivering meals with a daily menu.

ActiveRecord::Schema.define(version: 20171226222308) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "categorys", force: :cascade do |t| t.string "name" end create_table "items", force: :cascade do |t| t.string "name" t.decimal "price" t.bigint "category_id" t.string "image_url" t.bigint "weekday_id" t.index ["category_id"], name: "index_items_on_category_id" t.index ["weekday_id"], name: "index_items_on_weekday_id" end create_table "weekdays", force: :cascade do |t| t.string "name" t.date "dateday" end add_foreign_key "items", "categorys" add_foreign_key "items", "weekdays" end 

There are models:

 class Weekday < ApplicationRecord has_many :items, dependent: :destroy end class Item < ApplicationRecord belongs_to :category belongs_to :weekday validates :name, :price, presence: true end class Category < ApplicationRecord has_many :items def to_s name end end 

Controller:

 class WeekdaysController < ApplicationController before_action :set_weekday, only: [:show, :edit, :update, :destroy] def show @category = Category.all if params[:category_id].present? @current_category = Category.find(params[:category_id]) end ... 

Views Weekday and show.html.erb :

 <p id="notice"><%= notice %></p> <p> <strong>Name:</strong> <%= @weekday.name %> </p> <p> <strong>Dateday:</strong> <%= @weekday.dateday %> </p> <h1>Menu </h1> <div class="btn-group"> <% @category.each do |category| %> <%= link_to category.name, weekday_path(category_id: category.id), class: "btn #{params[:category_id] == category.id.to_s ? 'btn-primary' : 'btn-secondary'}"%> <% end %> </div> <% if @current_category %> <table class="table"> <thead> <tr> <th>name</th> <th>price</th> <th>image</th> <th></th> <th></th> </tr> </thead> <tbody> <div class="tbody"> <% @current_category.items.each do |item| %> <tr> <td><%= item.name %></td> <td><%= number_to_currency item.price %></td> <td><%= image_tag item.image_url %></td> <td><%= link_to "Edit", edit_weekday_item_path(weekday_id: @weekday.id,id: item.id) %></td> <td><%= link_to "Delete", [@weekday, item], method: :delete, conform: "Srly?"%></td> </tr> <% end %> </div> </tbody> </table> <% else %> <div class="alert alert-info"> PLS select a course PLS</div> <% end %> <%= link_to 'Edit', edit_weekday_path(@weekday) %> <%= link_to 'Back', weekdays_path %> <%= link_to "Add new item", new_weekday_item_path(@weekday)%> 

When switching to Weekday should display the Items belonging to the Navs, Category - (First,Second, Drink) day and located in a specific Navs, Category - (First,Second, Drink) . Currently, Navs belonging to the Category displayed in the Navs , but without checking for belonging to Weekday .

How to make this check so that in a certain category the items belonging to a particular Weekday and Category displayed? In other words, how to display the Model Item which simultaneously belongs to two unrelated models?

    1 answer 1

    If I correctly understood the task, then it is worth using scope. You need to output items with a defined category_id and weekday_id

     scope :by_category, ->(category_id) { where(category_id: category_id) } scope :by_weekday, ->(weekday_id) { where(weekday_id: weekday_id) } 

    By placing this code in the Item model, you can select the desired items, for example in the controller:

     @items = Item.by_category(params[:category_id]).by_weekday(params[:weekday_id]) 
    • Thank you very much, because of the small development experience, I did not know about such things. Thanks again!!! - Vladimir Konceropyaty
    • @ Vladimir Konzeropyatov, I am glad that everything worked :) You can read more about scopes here - Vasilisa