Option_groups_from_collection_for_select a specific group of methods

I want to make a dynamic selection form in ActiveAdmin. Let's say the user has several orders. And every order has a pay (boolean) attribute. After selecting the User, the next selecton is the payment for the order by this user.

option_groups_from_collection_for_select(User.order(:email), :orders, :email, :id, :name)

But I do not understand how to display only paid orders.

+3
source share
1 answer

You can define a method in the model Userthat replaces the argument :ordersthat you pass. This method should return paid orders.

Try using an association declaration (this will add you a method):

class User < ActiveRecord::Base
  has_many :orders
  has_many :paid_orders, class_name: 'Order', -> { where pay: true }
end

User paid_orders, orders, pay true.

:

option_groups_from_collection_for_select(User.order(:email), :paid_orders, :email, :id, :name)

.

, :paid_orders - User. , , :

class User < ActiveRecord::Base
  has_many :orders

  def paid_orders
    orders.where pay: true
  end
end
+3

All Articles