Rails:使用以整数开头的属性名编译错误

时间:2012-01-12 20:15:57

标签: ruby-on-rails compilation integer

我遇到编译错误。你能帮助我得到它吗?这在我看来(特别是5prime_primer的第三行):

 <tr>
      <td><%=relation.AmpInfoName%></td>
      <td><%=relation.5prime_primer%></td>
      <td><%=relation.3prime_primer%></td>
      <td><%=relation.Selective_bases_1%></td>
      <td><%=relation.Selective_bases_2%></td>
 </tr>

产生此错误:

compile error
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:314: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.5prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:314: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.5prime_primer);@output_buffer.s...
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:315: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.3prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:315: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.3prime_primer);@output_buffer.s...
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:338: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.5prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:338: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.5prime_primer);@output_buffer.s...
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:339: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.3prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:339: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.3prime_primer);@output_buffer.s...
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:354: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.5prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:354: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.5prime_primer);@output_buffer.s...
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:355: no .<digit> floating literal anymore; put 0 before dot
...tput_buffer.append= (relation.3prime_primer);@output_buffer....
                              ^
/usr/home/benjamin/locus/app/views/locus_table/show.html.erb:355: syntax error, unexpected tINTEGER
...put_buffer.append= (relation.3prime_primer);@output_buffer.s...

你能帮助我弄清楚如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

Ruby方法名称不能以数字开头。但是,您可以定义自己的访问者:

class Foo < ActiveRecord::Base
  def three_prime_primer
    read_attribute '3_prime_primer'
  end

  def three_prime_primer=(value)
    write_attribute '3_prime_primer', value
  end
end

用一个小方法包装它并不难,这样你就可以

access_attribute '3_prime_primer', :as => 'three_prime_primer'

答案 1 :(得分:0)

我在模型中定义了一个方法,然后使用hashmap查找属性:

class AmplificationInfoTable < ActiveRecord::Base
        attr_accessor :all
        def fiveprime_primer
                attributes["5prime_primer"]
        end
end

这让我可以做以下事情:

relation.fiveprime_primer

和平!