【问题标题】:SQLite3::SQLException: no such column: problem with association and show viewSQLite3::SQLException:没有这样的列:关联和显示视图有问题
【发布时间】:2011-07-19 11:20:25
【问题描述】:

我在视图上收到以下错误消息:http://localhost:3000/microposts/2 SQLite3::SQLException:没有这样的列:answers.micropost_id: SELECT "answers".* FROM "answers" WHERE ("answers".micropost_id = 2)

这是我的迁移:

| db > 迁移 > 创建微博 |

class CreateMicroposts < ActiveRecord::Migration
  def self.up
    create_table :microposts do |t|
      t.string :content

      t.timestamps
    end
  end

  def self.down
    drop_table :microposts
  end
end

| db > 迁移 > 创建答案 |

class CreateAnswers < ActiveRecord::Migration
  def self.up
    create_table :answers do |t|
      t.string :location
      t.text :body
      t.references :micropost
      t.integer :micropost_id

      t.timestamps
    end

  end

  def self.down
    drop_table :answers
  end
end

答案控制者:

def create

    @answer = Answer.new(params[:answer])
    @answer.micropost = @micropost; @answer.save && @micropost.save


    redirect_to micropost_path(@micropost)




    respond_to do |format|
      if @answer.save
        format.html { redirect_to(@answer, :notice => 'Answer was successfully created.') }
        format.xml  { render :xml => @answer, :status => :created, :location => @answer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @answer.errors, :status => :unprocessable_entity }
      end
    end
  end

和视图:

<p id="notice"><%= notice %></p>

<p>
  <b>Content:</b>
  <%= @micropost.content %>
  <h2>Location Answers:</h2>
  <% @micropost.answers.each do |answer| %>

    <p>
      <b>Answer:</b>
      <%= answer.body%>
    </p>
    <%end %>

  <h2> Answer a location:</h2>
  <%= form_for ([@micropost, @micropost.answers.build]) do |f| %>
    <div class="field">
      <%= f.label :location %><br />
      <%= f.text_field :location %>
    </div>
    <div class="field">
      <%= f.label :body %><br/>
      <%= f.text_area :body %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
    <% end %>

</p>

<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
<%= link_to 'Back', microposts_path %>

我找不到这个应用程序有什么问题。 - 我试图回滚并再次迁移它没有工作。 - 我试图在迁移中手动添加“t.integer:micropost_id”,但它不起作用 我的模型有关联“belongs_to”和“has_many”,我添加了“ 资源:微博做 资源:答案 结尾 到我的 config.rb 文件中。

【问题讨论】:

    标签: ruby-on-rails sqlite comments associations


    【解决方案1】:

    我相信 has_many 关联需要一个关系表来加入。如果您有 has_one 和 belongs_to(一对一关联),则可以使用简单的 _id 列方法,但 has_many 不会。所以如果你把这个连接表放进去,通过它来描述has_many,你应该得到你想要的。

    这是一个非常棒的 rails associations 指南,当我不清楚这些内容时,我会使用它。

    【讨论】:

      【解决方案2】:

      您无需在迁移中设置 micropost_id。这是由 t.references

      完成的
      create_table :answers do |t|
        t.string :location
        t.text :body
        t.references :micropost
      
        t.timestamps
      end
      

      设置和索引的最佳实践,例如:

      add_index :answers, :micropost_id
      

      【讨论】:

        猜你喜欢
        • 2016-01-19
        • 2022-08-15
        • 2013-10-09
        • 1970-01-01
        • 2020-10-18
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多