Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 选项\u组\u从\u集合\u为\u选择反转?_Ruby On Rails - Fatal编程技术网

Ruby on rails 选项\u组\u从\u集合\u为\u选择反转?

Ruby on rails 选项\u组\u从\u集合\u为\u选择反转?,ruby-on-rails,Ruby On Rails,我有两种型号: class Account < ActiveRecord::Base has_many :cost_centres end 谢谢 顺便说一句,我确实看了一下这里:-因为我认为这符合我的需要,但我没能让它工作。我想这应该适合你 cost_centres = CostCentre.where(id: [1,2,3]).pluck(:account_id) @accounts = Account.where('id in ?', cost_centres) <%=

我有两种型号:

class Account < ActiveRecord::Base
  has_many :cost_centres
end
谢谢


顺便说一句,我确实看了一下这里:-因为我认为这符合我的需要,但我没能让它工作。

我想这应该适合你

cost_centres = CostCentre.where(id: [1,2,3]).pluck(:account_id)
@accounts = Account.where('id in ?', cost_centres)

<%= select_tag :cost_centre, option_groups_from_collection_for_select(@accounts, :cost_centres, :title, :id, :name), { prompt: t(:select_a_cost_centre), class: %w(updateOptions col-md-12), :data => { :element => '#cost_centre_options' } } %>

这不合适,因为它暴露了用户无法访问的成本中心。i、 e.“成本中心A”、“成本中心B”和“成本中心C”均属于“账户1”。我的用户可以访问A和B,但不能访问C-但按照您的建议,用户最终将访问所有三个成本中心。
@accounts = Account.where(id: [1,2,3])

<%= select_tag :cost_centre, option_groups_from_collection_for_select(@accounts, :cost_centres, :title, :id, :name), { prompt: t(:select_a_cost_centre), class: %w(updateOptions col-md-12), :data => { :element => '#cost_centre_options' } } %>
<select name="cost_centre" id="cost_centre" class="updateOptions col-md-12" data-element="#cost_centre_options">
    <option value="">Select A Cost Centre</option>
    <optgroup label="Conn, Gutkowski and Pouros">
        <option value="11117">Becker Inc</option>
        <option value="11119">Batz Inc</option>
        <option value="11127">Konopelski Inc</option>
    </optgroup>
    <optgroup label="Tillman, Collins and Kautzer">
        <option value="11136">Batz-Willms</option>
        <option value="11141">Schaefer, Reilly and Miller</option>
        <option value="11138">Paucek-Hammes</option>
    </optgroup>
</select>
@cost_centres = CostCentre.where(id: [1,2,3])
cost_centres = CostCentre.where(id: [1,2,3]).pluck(:account_id)
@accounts = Account.where('id in ?', cost_centres)

<%= select_tag :cost_centre, option_groups_from_collection_for_select(@accounts, :cost_centres, :title, :id, :name), { prompt: t(:select_a_cost_centre), class: %w(updateOptions col-md-12), :data => { :element => '#cost_centre_options' } } %>