Activerecord RubyonRails 4:对嵌套属性进行排序

Activerecord RubyonRails 4:对嵌套属性进行排序,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我正在使用嵌套属性以一种形式更新父项和子项,并且我希望子项根据其源标题字段进行排序: ... <!-- Loop with details mappings data to update --> <table class="table table-striped table-condensed"> <tr align="left"> <th>Software</th> <th>Table</th&

我正在使用嵌套属性以一种形式更新父项和子项,并且我希望子项根据其源标题字段进行排序:

...
<!-- 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>
...
映射模型

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
类映射(myu pgnd){where“playerd\u id=?”,myu pgnd}
###过滤前
更新前:检索\u目标\u标题
###验证
...
验证:身份证,状态:真
属于:操场
验证:操场,状态:true#验证操场是否存在
属于:映射列表
属于:owner,:class\u name=>“User”,:foreign\u key=>“owner\u id”#帮助检索所有者名称
###专用函数定义
私有的
def检索_目标_标题
当前列表=self.mappings\u list.target\u list
当前值=当前值列表。值。其中(“值代码=?”,self.target\u代码)。获取!
self.target\u caption=当前值.value\u caption
结束
结束
映射列表模型

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
类映射列表(myu pgnd){where“playerd\u id=?”,myu pgnd} ###过滤前 创建前:设置代码 ###后过滤器 创建后:生成映射 ###验证 ... 验证:身份证,状态:真 属于:操场 验证:操场,状态:true#验证操场是否存在 属于:owner,:class\u name=>“User”,:foreign\u key=>“owner\u id”#帮助检索所有者名称 属于:source\u list,:class\u name=>“ValuesList”,:foreign\u key=>“source\u list\u id”#帮助检索源列表名称 属于:target_list,:class_name=>“ValuesList”,:foreign_key=>“target_list_id”#帮助检索目标列表名称 有很多:映射 接受:映射的\u嵌套\u属性\u ###专用函数定义 私有的 ###过滤器前 def set_代码 self.code=“#{source_list.code}{u到#{target_list.code}” 结束 ###后过滤器 def build_映射 self.source_list.values.each do | mapping_值| self.mappings.build(:playder\u id=>self.playder\u id,:source\u software=>mapping\u value.values\u list.software\u name,:source\u table=>mapping\u value.value\u code,:source\u caption=>mapping\u value.value\u caption,:target\u software=>self.target\u list.software\u name,:target\u table=>self.target\u list.table\u name,:created_by=>self.created_by,:updated_by=>self.updated_by) 结束 结束 结束 注意:创建映射列表时,它会生成映射。然后可以使用其嵌套映射编辑映射列表

谢谢你的帮助

致以最良好的祝愿


Fred

请共享父模型和子模型以及架构的详细信息。此外,请共享将数据传递到此视图的控制器操作。更新问题本身。嗨,Kirti Thorat,对嵌套属性排序有何想法?谢谢。
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