Ruby on Rails 4:排序嵌套属性

时间:2014-03-17 23:29:01

标签: activerecord ruby-on-rails-4

我正在使用嵌套属性来更新一个表单中的父级和子级,我希望子级按照source_caption字段进行排序:

...
<!-- Loop with details mappings data to update -->

  <table class="table table-striped table-condensed">
    <tr align="left">
  <th>Software</th>
  <th>Table</th>
  <th>Value</th>
  <th>Value</th>
  <th>Table</th>
  <th>Software</th>
    </tr>
    <%= f.fields_for :mappings do |m| %>
    <tr align="left">
    <td><%= m.text_field :source_software, :readonly => true %></td>
    <td><%= m.text_field :source_table, :readonly => true %></td>
    <td><%= m.text_field :source_caption, :readonly => true %></td>
    <td><%= m.collection_select :target_code, @target_values, :value_code, :value_caption %></td>
    <td><%= m.text_field :target_table, :readonly => true %></td>
    <td><%= m.text_field :target_software, :readonly => true %></td>
    </tr>
    <% end%>
  </table>
...

默认情况下,它们会根据记录的上次更新日期进行排序。 如何按source_caption字段对它们进行排序?

更新

控制器调用视图:MappingListsController

# GET /mappings_list/1/edit
  def edit
      @mappings_list = MappingsList.pgnd(current_playground).find(params[:id])
  end

映射模型

class Mapping < ActiveRecord::Base

### scope
  scope :pgnd, ->(my_pgnd) { where "playground_id=?", my_pgnd }

### before filter
before_update :retrieve_target_caption

### validation
...
validates :playground_id, presence: true
    belongs_to :playground
validates :playground, presence: true                       # validates that the playground exists
    belongs_to :mappings_list
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"        # helps retrieving the owner name

### private functions definitions
  private

  def retrieve_target_caption
    current_list = self.mappings_list.target_list
    current_value = current_list.values.where("value_code = ?", self.target_code).take! 
    self.target_caption = current_value.value_caption
  end

end

MappingsList模型

class MappingsList < ActiveRecord::Base

### scope
  scope :pgnd, ->(my_pgnd) { where "playground_id=?", my_pgnd }

### before filter
  before_create :set_code

### after filter
  after_create :build_mappings

### validation
...
    validates :playground_id, presence: true
    belongs_to :playground
    validates :playground, presence: true                   # validates that the playground exists
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"    # helps retrieving the owner name
    belongs_to :source_list, :class_name => "ValuesList", :foreign_key => "source_list_id" # helps retrieving the source list name
    belongs_to :target_list, :class_name => "ValuesList", :foreign_key => "target_list_id" # helps retrieving the target list name
    has_many :mappings
    accepts_nested_attributes_for :mappings

### private functions definitions
  private

  ### before filters
    def set_code 
      self.code = "#{source_list.code}_TO_#{target_list.code}"
    end 

  ### after filters
    def build_mappings
      self.source_list.values.each do |mapping_value|
        self.mappings.build(:playground_id => self.playground_id, :source_software => mapping_value.values_list.software_name, :source_table => mapping_value.values_list.table_name, :source_code => mapping_value.value_code, :source_caption => mapping_value.value_caption, :target_software => self.target_list.software_name, :target_table => self.target_list.table_name, :created_by => self.created_by, :updated_by => self.updated_by)
      end
    end

end

注意:创建映射列表时,会生成映射。然后可以使用嵌套映射编辑映射列表。

感谢您的帮助,

致以最诚挚的问候,

佛瑞德

0 个答案:

没有答案