Excuse me for the noob question and english.I'm a newbie to Ruby on Rails.My rails application has a Reservation system and a Bookings page to view all the Bookings.The Rails App has three types of user roles, they are:
I have User, Clients, Booking models
class User < ApplicationRecord belongs_to :clients has_many :bookings end class Clients < ApplicationRecord has_many :users, :dependent => :destroy end class Booking < ApplicationRecord belongs_to :user end
A user with supervisor_role can view all the reservations of the users which belong to the same client. (ex: suppose James belongs to a client(ex: ABC) and also has a supervisor_role.Now James can view(access) all the reservations of all the users belonging to the client( ABC).
create_table "users", id: :bigserial, force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.boolean "supervisor_role" t.boolean "user_role" t.boolean "agent_role", default: true t.bigint "clients_id" end create_table "clients", id: :bigserial, force: :cascade do |t| t.string "name" t.string "email" t.string "access_token" t.string "access_secret" t.string "address" t.string "promo_code" end create_table "bookings", id: :bigserial, force: :cascade do |t| t.integer "user_id" t.string "pickup_date" t.string "pickup_time" t.string "dropff_date" t.string "dropff_time" t.string "confirmation_number" end
Now in the controller
def bookings if current_user.supervisor_role? client = current_user.clients users = User.where(clients_id: client.id) @bookings =  users.each_with_index do |u| u.bookings.each do |b| @bookings << b end end elsif current_user.user_role? || current_user.agent_role? @bookings = current_user.bookings elsif current_user.superadmin_role? redirect_to rails_admin_path end
I am able to fetch the booking results corresponding to respective user's roles(if current_user.supervisor_role? or current_user.user_role? || current_user.agent_role?).Everything is working upto now.
Now I need to Integrate Ransack search into the bookings action(already filtered according to the user_roles) of the controller for searching by pickup and dropoff dates, confirmation number...etc but I'm unable to add Ransack search into the bookings action.
I tried to follow the below tutorials
def index @q = Product.ransack(params[:q]) @products = @q.result @search = Product.search(params[:q]) @products = @search.result end
In the example above, the methods ransack and search are called upon the Model Class Name(Product).Can I call the methods (search or ransack) upon the ActiveRecord:: Relation instance(@bookings)?
How can I do Ransack search and sorting to my bookings action?
Any Help or suggestion is highly appreciated.
Thanks in advance!!ruby-on-railsrubyransack