Reddirt
Reddirt

Reputation: 5953

Rails grouped collection select limit list

I have a grouped_collection_select where I would like to limit the records displayed to tasks that are not closed. I can easily filter the records displayed at the opt group of the collection list. But, I can't filter the option level records (group_method).

This is in my Task model:

  scope :tasknotclosed, where("taskstatus_id != (?)", 3) 

This is the current select:

<%= f.grouped_collection_select :task_id, Workorder.wonotclosed.order(:description), :tasks, :description, :id, :taskname, :include_blank => 'Select:' %>

Instead of :tasks, I would like to use something like Task.tasknotclosed.all

But, that gives me:

syntax error, unexpected '['

Thanks for the help!

UPDATE 1

I tried adding this to the task model:

def tasknotclosed?
  self.taskstatus_id != 3
end 

And changed the select to:

<%= grouped_collection_select(:tasks, :task_id, Workorder.wonotclosed.order(:id), :task.tasknotclosed?, :description, :id, :taskname, :include_blank => true) %>

But I get:

undefined method `tasknotclosed?' for :task:Symbol

UPDATE2

I changed the select in the Events form to this:

<%= f.grouped_collection_select :task_id, @workorderlist, @tasklist, :description, :id, :taskname, :include_blank => 'Select:' %>

And the Events controller to this:

 def new
  @event = Event.new
  @tasklist = Task.where("showtimer == (?)", true).order(:taskname)
  @workorderlist = Workorder.where("wostatus_id != (?)", 15).order(:description)

The @workorderlist works the way I expected.

But, the @tasklist gives me

syntax error, unexpected $end

Upvotes: 2

Views: 687

Answers (2)

ussferox
ussferox

Reputation: 490

So I'm not sure where you are on this after previous comments and answer, but assuming your post above still stands, it looks like you're doing grouped_collection_select wrong. According to the docs, it should be more like

grouped_collection_select :event, :task_id, @workorderlist, @tasklist, :description, :id, :taskname

But part of the problem may be using an instance variable (@tasklist) when there should be a method there. The fourth element needs to be a group_method (= "The name of a method which, when called on a member of collection, returns an array of child objects representing the tags.") You can't call @tasklist on a member of your collection. So I would suggest making this an object method in your work_order model (assuming work_order has_many :tasks), like so:

class WorkOrder
  def tasklist
    tasks.where(<conditions>)
  end
end

so that then you can actually call the method :tasklist in that fourth position there.

Upvotes: 6

Igor Kasyanchuk
Igor Kasyanchuk

Reputation: 774

  1. Try to generate by your own needed Array with options, it must looks something like -

    ['North America', [['United States','US'],'Canada']], ['Europe', ['Denmark','Germany','France']]

then use grouped_options_for_select

  1. Maybe you can try to use simple_form gem.

  2. Code scope :tasknotclosed, where("taskstatus_id != (?)", 3) - try to implement it as instance method of class Workorder, e.g. def tasknotclosed

Upvotes: 0

Related Questions