user3331154
user3331154

Reputation: 187

How to use the results of one table found using form params to query another table - Ruby on Rails

I need to use the result of a query, found using form parameters to query another table and I am having difficulties.

The first query is:

       @criteria_event = CriteriaEvent.joins(:events).where("events.horse_id = ? AND events.task_group_id = ? AND events.start_date = ?", criteria_event_params[:horse_id], criteria_event_params[:task_group_id], criteria_event_params[:start_date])

This query returns the correct information as follows:

    [#<CriteriaEvent id: 57, task_group_id: 3, start_time: "2000-01-01 10:00:00", created_at: "2016-02-12 01:36:54", updated_at: "2016-02-12 01:36:54", start_date: "2016-02-23", horse_id: 13>]>

I have been trying to return the related events using the CriteriaEvent id: found above (id:57) in the following query. This query follows the above query immediately:

   @events = Event.where(criteria_event_id: @criteria_event.id)

I get the following error:

  NoMethodError (undefined method `id' for #<CriteriaEvent::ActiveRecord_Relation:

The relevant models have the following:

  class CriteriaEvent < ActiveRecord::Base
      has_many :events
      accepts_nested_attributes_for :events
  end

  class Event < ActiveRecord::Base
      belongs_to :criteria_event
   end

The controller has the following permits:

   def criteria_event_params
       params.require(:criteria_event).permit(:horse_id, :task_group_id, :start_date, :start_time, events_attributes: [:id, :criteria_event_id])
   end


   def event_params
      params.require(:event).permit(:title, :start_date, :start_time, :end_date, :end_time, :location, :description, :notification_type, :notification_span_before, :notification_span_type, :horse_id, :task_group_id, :criteria_event_id, criteria_events_attributes: [:criteria_event_id, :id])
   end

What am I missing?

Thanking you in advance for your help

Upvotes: 1

Views: 67

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

NoMethodError (undefined method `id' for #

The issue is that @criteria_event contains multiple records (inside an ActiveRecord::Relation object). This works like an array, and as such you don't have access to id on its own.

Instead, you'll need to either use pluck(:id) to get an array of ids, or just find_by (only returning a single result):

#pluck
@criteria_event = CriteriaEvent.joins(:events).where(events: { horse_id: criteria_event_params[:horse_id], task_group_id: criteria_event_params[:task_group_id], start_date criteria_event_params[:start_date]}).pluck(:id)

#find_by
@criteria_event = CriteriaEvent.joins(:events).find_by(events: { horse_id: criteria_event_params[:horse_id], task_group_id: criteria_event_params[:task_group_id], start_date: criteria_event_params[:start_date] }).id

Both of these will then allow you to use:

@events = Event.where(criteria_event_id: @criteria_event)

Upvotes: 1

Vishal Jain
Vishal Jain

Reputation: 1940

Hey you are trying to select id on active record relation you can use map for array of id as,

 @events = Event.where(criteria_event_id: @criteria_event.map(&:id))

Instead of using separate query you can find Events in this way,

@events= Event.joins(:criteria_event).where("events.horse_id = ? AND events.task_group_id = ? AND events.start_date = ?", criteria_event_params[:horse_id], criteria_event_params[:task_group_id], criteria_event_params[:start_date])

Upvotes: 2

Related Questions