【问题标题】:Rails not porting SQLITE localhost to Heroku PGSQLRails 没有将 SQLITE localhost 移植到 Heroku PGSQL
【发布时间】:2016-10-27 23:30:57
【问题描述】:

按照关于 Rails 的教程,我在尝试 method: :delete 操作时收到此错误。

ActiveRecord::StatementInvalid (PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer

这是我的销毁方法。

  def destroy
@user_stock = current_user.user_stocks.where(stock_id: params[:id]).first
@user_stock.destroy
respond_to do |format|
  format.html { redirect_to my_portfolio_path, notice: "#{@user_stock.stock.ticker} was removed from portfolio." }
  format.json { head :no_content }
end
end

我在here 上阅读了一个关于findfind_by 需要正确使用的问题,但这让我很困惑。

UserStocksController

class UserStocksController < ApplicationController
  before_action :set_user_stock, only: [:show, :edit, :update, :destroy]

  # GET /user_stocks
  # GET /user_stocks.json
  def index
    @user_stocks = UserStock.all
  end

  # GET /user_stocks/1
  # GET /user_stocks/1.json
  def show
  end

  # GET /user_stocks/new
  def new
    @user_stock = UserStock.new
  end

  # GET /user_stocks/1/edit
  def edit
  end

  # POST /user_stocks
  # POST /user_stocks.json
  def create
    if params[:stock_id].present?
      @user_stock = UserStock.new(stock_id: params[:stock_id], user: current_user)
    else
      stock = Stock.find_by_ticker(params[:stock_ticker])
      if stock
        @user_stock = UserStock.new(user: current_user, stock: stock)
      else
        stock = Stock.new_from_lookup(params[:stock_ticker])
        if stock.save
          @user_stock = UserStock.new(user:current_user, stock: stock)
        else
          @user_stock = nil
          flash[:error] = "Stock is not available"
        end
      end
    end
    
    respond_to do |format|
      if @user_stock.save
        format.html { redirect_to my_portfolio_path, notice: "Stock #{@user_stock.stock.ticker} was saved" }
        format.json { render :show, status: :created, location: @user_stock }
      else
        format.html { render :new }
        format.json { render json: @user_stock.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /user_stocks/1
  # PATCH/PUT /user_stocks/1.json
  def update
    respond_to do |format|
      if @user_stock.update(user_stock_params)
        format.html { redirect_to @user_stock, notice: 'User stock was successfully updated.' }
        format.json { render :show, status: :ok, location: @user_stock }
      else
        format.html { render :edit }
        format.json { render json: @user_stock.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /user_stocks/1
  # DELETE /user_stocks/1.json
  def destroy
    @user_stock = current_user.user_stocks.where(stock_id: params[:id]).first
    @user_stock.destroy
    respond_to do |format|
      format.html { redirect_to my_portfolio_path, notice: "#{@user_stock.stock.ticker} was removed from portfolio." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
  def set_user_stock
     @user_stock = UserStock.find_by(params[:id])
   end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_stock_params
      params.require(:user_stock).permit(:user_id, :stock_id)
    end
end

my_portfolio 视图呈现部分_list

<table class="table table-striped">
	<thead>
		<tr>
			<th>Name</th>
			<th>Symbol</th>
			<th>Current Price</th>
			<% if @user.id == current_user.id%>
			<th>Actions</th>
			<%end%>
		</tr>
	</thead>
	<tbody>
		<%@user_stocks.each do |us|%>
		<tr>
			<td><%=us.name%></td>
			<td><%=us.ticker%></td>
			<td><%=us.price%></td>
			<% if @user.id == current_user.id%>
			<td>
				<%= link_to 'Delete', user_stock_path(us), :method => :delete,
					 									   :data => {:confirm => "You sure you want to delete #{us.name} from your portfolio?"},
														   :class => "btn btn-xs btn-danger"%>
            </td>
			<%end%>
		</tr>
		<%end%>
	</tbody>
</table>

【问题讨论】:

    标签: ruby-on-rails postgresql sqlite heroku rails-activerecord


    【解决方案1】:

    好吧,我的猜测是这样。同样,您只发布了部分内容,所以我不确定这一切是如何相关的,但我的猜测是您添加了这样的 id:

    <%= link_to 'Delete', user_stock_path(id: us.id), :method => :delete,
                                                           :data => {:confirm => "You sure you want to delete #{us.name} from your portfolio?"},
                                                           :class => "btn btn-xs btn-danger"%>
    

    【讨论】:

    • 未定义方法stock_id
    • 你能发布你的整个控制器吗?
    • 刚刚在我的问题中做到了
    • ActiveRecord::StatementInvalid (PG::DatatypeMismatch: ERROR: WHERE 的参数必须是布尔类型,而不是在 heroku 中的整数类型
    • 你能不能试试最新的答案。如果它不起作用,请发布您的完整跟踪
    【解决方案2】:

    您的问题可能是您的set_user_stock 方法:

    def set_user_stock
      @user_stock = UserStock.find_by(params[:id])
    end
    

    UserStock.find_by(params[:id]) 只是一种更简短的说法:

    UserStock.where(params[:id]).first
    

    因此,如果 params[:id]6,您最终会将此 SQL 发送到数据库:

    select *
    from user_stocks
    where 6
    

    你的错误是:6 不是 SQL 中的布尔值,where 6 没有意义。 SQLite 对 SQL 的解释相当松散,所以它会接受像 where 6 这样的废话,但 PostgreSQL 抱怨是正确的。

    这里有两个教训:

    1. 您的set_user_stock 方法应如下所示:

      def set_user_stock
        @user_stock = UserStock.find(params[:id])
      end
      

      def set_user_stock
        @user_stock = UserStock.find_by(:id => params[:id])
      end
      

      如果您希望始终使用有效的params[:id]find_by 调用set_user_stock,请使用find 版本,如果您想自己处理@user_stock.nil?

    2. 永远不要在一个数据库之上开发并在另一个数据库上部署。如果您尝试在 SQLite 上进行开发并部署在其他任何东西上,那就更是如此。您应该始终使用同一个数据库进行开发、测试和部署(一直到数据库版本),ActiveRecord 不会保护您免受数据库可移植性的任何困难部分的影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 2011-01-17
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多