Reputation: 7003
I have the following setup:
Table.rb
has_many: orders
Order.rb
belongs_to: table
And the order
table has a boolean
value of finished
and I have this script:
collection_select(:order, :table_id, Table.all, :id, :table, prompt: false)
what I need to do is only select those tables that have orders where all orders are finished. So
if all orders for this table have order.finished == true OR if it doesn't have any orders at all { list that table }
How could I do this?
Upvotes: 0
Views: 265
Reputation: 3258
you need to make a method in table.rb which returns those tables who have orders and are finished or those tables who dont have orders.
def self.table_with_finished_or_no_orders
Table.joins("left join orders on orders.table_id=tables.id).where("orders.finished = ? OR orders.table_id is NULL", true)
end
and you will call this method in views like this
collection_select(:order, :table_id, Table.table_with_finished_or_no_orders, :id, :table, prompt: false)
i guess my query is according to your requirement.
Upvotes: 1