Ruby on Rails关联错误

时间:2011-07-20 17:40:45

标签: ruby-on-rails many-to-many relational-database

我创建了多对多关联rails项目,并在运行new.html.erb时遇到以下错误:

Table 'purchase_form_rails.account_numbers_pi_names' doesn't exist

undefined local variable or method `account_numbers' for #<#<Class:0x2a979ef4>:0x2a978770>

Extracted source (around line #34): 

(应该在数据库中查看purchase_form_rails,以及表account_numbers(在表中加入pi_names和account_numbers之间是pi_names_account_numbers)

继承我的new.html.erb:

  <div class="field">

        <% for account_number in @account_numbers %>
           <input type="checkbox"
                id="<%=account_number.id%>"
                name="account_numbers_ids[]"
                value="<%=account_number.id %>"
                <%if @pi_name.account_numbers.include? tag%>checked="checked"<%end%>
         > <%=account_numbers.account_number%>
        <% end %>

  </div>

如果您需要更多信息,请与我们联系!

2 个答案:

答案 0 :(得分:1)

对于多对多关联,rails约定期望连接/链接表按字母顺序排列,你说

  

加入pi_names和account_numbers的表之间是pi_names_account_numbers

但错误说

  

account_numbers_pi_names'不存在

将表名从'pi_names_account_numbers'更改为'account_numbers_pi_names'应解决问题

答案 1 :(得分:1)

如果要定义has_many:thorough,那么rails期望连接表名称应按字母顺序排序,如account_numbers_pi_names(这应该是连接表名称而不是pi_names_account_numbers)。