Ruby on rails 3 Rails:访问使用列名称选择的列的值

Ruby on rails 3 Rails:访问使用列名称选择的列的值,ruby-on-rails-3,hash,Ruby On Rails 3,Hash,你知道要怎么做才能让它工作吗?我一辈子都想不出来 def get_prices(c) @print_prices = {} Billing.where(:name => c).column_names.each do |d| if d.match(/^print_/) @print_prices[d] = d.value end end return @print_prices end 我不知道用d.value代替什么 欢迎提供帮助。以下代码将

你知道要怎么做才能让它工作吗?我一辈子都想不出来

def get_prices(c)
  @print_prices = {}
  Billing.where(:name => c).column_names.each do |d|
    if d.match(/^print_/)
      @print_prices[d] = d.value
    end
  end
  return @print_prices
end
我不知道用
d.value
代替什么


欢迎提供帮助。

以下代码将执行查询,以关系的形式返回,并拒绝属性键值哈希中与给定正则表达式不匹配的所有项,在本例中,该正则表达式为
/^print\

def get_prices(c)
  Billing.where(:name => c).first.attributes.reject{ |i| !i.match(/^print_/) }
end
或者,它也可以写成:

def get_prices(c)
  Billing.where(:name => c).first.attributes.select{ |i| i.match(/^print_/) }
end

下面的代码将执行以关系形式返回的查询,并拒绝属性键值哈希中与给定正则表达式不匹配的所有项,在本例中,该正则表达式为
/^print./

def get_prices(c)
  Billing.where(:name => c).first.attributes.reject{ |i| !i.match(/^print_/) }
end
或者,它也可以写成:

def get_prices(c)
  Billing.where(:name => c).first.attributes.select{ |i| i.match(/^print_/) }
end

你到底想在这里做什么?我正试图构建一个散列值,即,
{“print\u 100”=>“30”、“print\u 200”=>“60”、“print\u 500”=>“90”}
。键是列名,值是列的值。您希望
计费。其中(:name=>c)
只返回关系中的一条记录?您到底想在这里做什么?我正在尝试构建值的散列,即
{“print\u 100=>”30”,“print\u 200=>”60”,“print\u 500=>”90“}
。键是列名,值是列的值。您希望
计费。其中(:name=>c)
只返回关系中的一条记录?干杯!效果很好。是的,双重否定总是让人困惑。哈,干杯!效果很好。是的,双重否定总是让人困惑。哈