是否有一种简单的方法来运行单个迁移?我不想迁移到某个版本,我只想运行一个特定的版本。

我有一个功能正常的Rails 3应用程序,使用has_many:通过关联,这不是,因为我把它重新制作为Rails 4应用程序,让我保存id从相关的模型在Rails 4版本。

这是三个相关的模型是相同的两个版本。

Categorization.rb

class Categorization < ActiveRecord::Base

  belongs_to :question
  belongs_to :category
end

Question.rb

has_many :categorizations
has_many :categories, through: :categorizations

Category.rb

has_many :categorizations
has_many :questions, through: :categorizations

在两个应用中,类别id都像这样传递到创建动作中

  "question"=>{"question_content"=>"How do you spell car?", "question_details"=>"blah ", "category_ids"=>["", "2"],

在Rails 3应用程序中,当我创建一个新问题时,它会插入到问题表中,然后插入到分类表中

 SQL (82.1ms)  INSERT INTO "questions" ("accepted_answer_id", "city", "created_at", "details", "province", "province_id", "question", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["accepted_answer_id", nil], ["city", "dd"], ["created_at", Tue, 14 May 2013 17:10:25 UTC +00:00], ["details", "greyound?"], ["province", nil], ["province_id", 2], ["question", "Whos' the biggest dog in the world"], ["updated_at", Tue, 14 May 2013 17:10:25 UTC +00:00], ["user_id", 53]]
  SQL (0.4ms)  INSERT INTO "categorizations" ("category_id", "created_at", "question_id", "updated_at") VALUES (?, ?, ?, ?)  [["category_id", 2], ["created_at", Tue, 14 May 2013 17:10:25 UTC +00:00], ["question_id", 66], ["updated_at", Tue, 14 May 2013 17:10:25 UTC +00:00]]

在rails 4应用程序中,在它处理questioncontroller# create中的参数后,我在服务器日志中得到这个错误

Unpermitted parameters: category_ids

问题只是被插入到问题表中

 (0.2ms)  BEGIN
  SQL (67.6ms)  INSERT INTO "questions" ("city", "created_at", "province_id", "question_content", "question_details", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["city", "dd"], ["created_at", Tue, 14 May 2013 17:17:53 UTC +00:00], ["province_id", 3], ["question_content", "How's your car?"], ["question_details", "is it runnign"], ["updated_at", Tue, 14 May 2013 17:17:53 UTC +00:00], ["user_id", 12]]
   (31.9ms)  COMMIT

虽然我没有在Questions模型中存储category_ids,但我将category_ids设置为questions_controller中的一个允许参数

   def question_params

      params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids)
    end

有人能解释一下我应该如何保存category_ids吗?注意,在categorories_controller中没有创建动作。任意一个应用程序的Rb。

这三个表在两个应用中是一样的

 create_table "questions", force: true do |t|
    t.text     "question_details"
    t.string   "question_content"
    t.integer  "user_id"
    t.integer  "accepted_answer_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "province_id"
    t.string   "city"
  end

 create_table "categories", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "categorizations", force: true do |t|
    t.integer  "category_id"
    t.integer  "question_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

更新

这是Rails 3应用程序的创建操作

  def create
      @question = Question.new(params[:question])
      respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: "new" }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
end

这是Rails 4应用程序的创建操作

   def create
      @question = Question.new(question_params)

       respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: "new" }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
    end

这是question_params方法

 private
    def question_params 
      params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids)
    end

当我在控制台中执行命令时,是否有方法禁用SQL查询日志记录?理想情况下,如果我可以禁用它,并在控制台中使用命令重新启用它,那就太好了。

我试图调试一些东西,并使用“放”打印出一些相关数据。但是,sql查询输出使其难以阅读。


编辑: 我找到了另一个解决方案,因为设置记录器为nil有时会引发一个错误,如果我的代码之外的东西试图调用logger.warn

您可以将记录器的级别设置为1,而不是将记录器设置为nil。

ActiveRecord::Base.logger.level = 1 # or Logger::INFO

假设我有一个数组

[0, 132, 432, 342, 234]

去掉第一个元素最简单的方法是什么?(0)

在Rails环境中运行计划任务的最佳方法是什么?脚本/跑步吗?耙?我想每隔几分钟运行一次任务。

如何将此代码转换为原始sql并在rails中使用?因为当我在heroku中部署此代码时,有一个请求超时错误。我认为这将更快,如果我使用原始sql。

@payments = PaymentDetail.joins(:project).order('payment_details.created_at desc')
@payment_errors = PaymentError.joins(:project).order('payment_errors.created_at desc')

@all_payments = (@payments + @payment_errors)

我在mac上,做:

rails server

我得到:

2010-12-17 12:35:15] INFO  WEBrick 1.3.1
[2010-12-17 12:35:15] INFO  ruby 1.8.7 (2010-08-16) [i686-darwin10.4.0]
[2010-12-17 12:35:15] WARN  TCPServer Error: Address already in use - bind(2)
Exiting

我知道我可以在一个新的端口上启动一个进程,但是我想终止这个进程。

如何向通过迁移已经存在的列添加默认值?

我能找到的所有文档都向您展示了如何在列不存在的情况下做到这一点,但在这种情况下它存在。

有时,Activerecord数据类型让我感到困惑。经常犯错,。我的一个永恒的问题是,对于一个给定的情况,

我应该使用:decimal还是:float?

我经常遇到这个链接,ActiveRecord::decimal vs:float?,但答案还不够明确,我无法确定:

我见过很多人极力建议不要使用的线程 浮点数,总是使用小数。我也看到了一些建议 人们只在科学应用中使用浮子。

以下是一些例子:

地理位置/纬度/经度:-45.756688,120.5777777,… 比例/百分比:0.9,1.25,1.333,1.4143,…

我过去使用过:decimal,但我发现在Ruby中处理BigDecimal对象比处理float对象更尴尬。例如,我还知道我可以使用:integer来表示钱/分,但它不太适合其他情况,例如当精度随时间变化的数量时。

使用每种方法的优点/缺点是什么? 有什么好的经验法则来知道使用哪种类型?

我试图生成一个新模型,并忘记引用另一个模型的ID的语法。我自己也会去查,但我还没有在所有Ruby on Rails文档链接中找到确切的源代码。

项目名称:字符串描述:文本(这里是reference:product或references:product)。但更好的问题是,我在哪里或如何在未来轻松地寻找这种愚蠢?

注意:我已经知道,如果我输入错了其中一个选项并运行迁移,那么Ruby on Rails将完全搞砸我的数据库……而rake db:rollback对于这种错误是无能为力的。我确定我只是有些地方不明白,但在我明白之前…rails g模型返回的“详细”信息仍然让我抓挠…