rails关联的数组/哈希

时间:2015-05-27 17:34:45

标签: ruby-on-rails arrays ruby ruby-on-rails-4 hash

我是Ruby on Rails的新手,我没有让我的代码使用数组和哈希组合。

我查看了@ftp_accounts [p.id] .each:

- current_user.packets.each do |p|
  .row
    .widget.col-6
      .widget-header
        = fa_icon 'info'
        FTP-Accounts für das Paket: #{ p.name }
      .widget-body
        .widget-title
          Sie haben ##USED_FTP_ACCOUNTS## von #{ p.ftp_accs } genutzt.
        %p
          Um einen FTP-Account Ihrem Paket hinzuzufügen, klicken Sie bitte 
          %a{ href:'' }>hier
          \.

        %table
          %thead
            %tr
              %th #
              %th Benutzer
              %th Kommentar
              %th Verzeichnis
              %th Quota
              %th Aktion
          %tbody
            - if @ftp_accounts[p.id].nil?
              %tr
            - else
              - @ftp_accounts[p.id].each do |ftp_acc|
                %tr
                  %td
                    = ftp_acc.id
                  %td
                    = ftp_acc.username
                  %td
                    = ftp_acc.comment
                  %td
                    = ftp_acc.dir
                  %td
                    = number_to_human_size(ftp_acc.quota_size)
                  %td asd

使用- @ftp_accounts[p.id].each do |ftp_acc|我想显示@ftp_accounts [p.id]数组中的所有条目。

我使用= @ftp_accounts[p.id].inspect获得此输出:

{2=>#<FtpAccount id: 2, username: "example_2", status: 1, password: "88ea39439e74fa27c09a4fc0bc8ebe6d00978392", uid: 2000, gid: 2000, dir: "/home/test", ul_bandwidth: 100, dl_bandwidth: 100, comment: "Jop", ipaccess: "*", quota_size: 20, quota_files: 0, user_id: 1, packet_id: 1, created_at: nil, updated_at: nil>} 

我想要两个ftp帐户:

#<ActiveRecord::Associations::CollectionProxy [#<FtpAccount id: 1, username: "example", status: 1, password: "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", uid: 2000, gid: 2000, dir: "/home/customer/ftp60047", ul_bandwidth: 500, dl_bandwidth: 500, comment: "SEEDS ACCOUNT 2GB", ipaccess: "*", quota_size: 2048, quota_files: 0, user_id: 1, packet_id: 1, created_at: "2015-05-27 15:34:36", updated_at: "2015-05-27 15:34:36">, #<FtpAccount id: 2, username: "example_2", status: 1, password: "88ea39439e74fa27c09a4fc0bc8ebe6d00978392", uid: 2000, gid: 2000, dir: "/home/test", ul_bandwidth: 100, dl_bandwidth: 100, comment: "Jop", ipaccess: "*", quota_size: 20, quota_files: 0, user_id: 1, packet_id: 1, created_at: nil, updated_at: nil>]> 

我的控制器:

  def index
    @ftp_accounts = Hash.new

    current_user.ftp_accounts.to_ary.each do |ftp_acc|
      @ftp_accounts[ftp_acc.packet_id] = Hash.new
      @ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = Hash.new
      @ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = ftp_acc
    end
  end

我的user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  has_and_belongs_to_many :packets, :join_table => :users_packets, dependent: :destroy

  # BackUp
  has_many :backups, dependent: :destroy
  has_many :ftp_accounts, dependent: :destroy
end

和我的ftp_account.rb

class FtpAccount < ActiveRecord::Base

  belongs_to :user
  belongs_to :packet
end

和我的packet.rb

class Packet < ActiveRecord::Base

  has_and_belongs_to_many :users, :join_table => :users_packets
  belongs_to :resource, :polymorphic => true

  belongs_to :user
end

1 个答案:

答案 0 :(得分:1)

您的控制器逻辑是问题:

  def index
    @ftp_accounts = Hash.new

    current_user.ftp_accounts.to_ary.each do |ftp_acc|
      @ftp_accounts[ftp_acc.packet_id] = Hash.new # <-- this line is a problem
      @ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = Hash.new
      @ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = ftp_acc
    end
  end

如果循环中的第一行共享相同的packet_id,则会消除在先前迭代中放入变量的所有内容。

您可以将其更改为:

@ftp_accounts[ftp_acc.packet_id] ||= Hash.new

注意:循环中的第二行不执行任何操作,因为您要将值设置为新的哈希值,然后立即使用ftp_acc覆盖它。