我正在跟随rails教程.我在上.6我和SQLite3有一个奇怪的错误(为了记录,我使用的是sqlite 1.3.10版本,教程使用的是1.3.9)
运行rake db:migrate时我没有收到错误,但是当我为测试环境运行迁移时,我得到的是:
$ bundle exec rake test:models
rake aborted!
ActiveRecord::PendingMigrationError:
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=test
sample_app/test/test_helper.rb:3:in `<top (required)>'
sample_app/test/models/user_test.rb:1:in `require'
sample_app/test/models/user_test.rb:1:in `<top (required)>'
Tasks: TOP => test:models
(See full trace by running task with --trace)
$ bundle exec rake db:migrate RAILS_ENV=test
== 20150628011937 AddIndexToUsersEmail: migrating ===========================
==
-- add_index(:users, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: CREATE UNIQUE INDEX …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个按日期和school_id查找课程的创建操作.如果这些参数没有课程,那么我创建一个新的课程.我没有使用find_by_or_create或类似方法的原因是因为我想添加Lesson的目标(如果它存在),也不会覆盖它.
这是我的控制器,我相信所有这些都是相关的:
存在吗?call永远不会返回true,即使我运行rails console并使用相同的参数将其键入控制台,它也会返回true.与find_by相同
def create
find_or_create_lesson
if @lesson.save
flash[:success] = "Response logged"
redirect_to @lesson
else
@user = current_user
@school_options = School.all.map { |s| [s.name, s.id] }
render 'new'
end
end
private
# strong params
def lesson_params
params.require(:lesson).permit(:school_id, :date,
goals_attributes: [:id, :user_id,
:text, :lesson_id])
end
# Finds a lesson with the same date and school, or creates the lesson
def find_or_create_lesson
search_params = { date: lesson_params[:date],
school_id: lesson_params[:school_id] }
if Lesson.exists?(search_params)
@lesson = Lesson.find_by(search_params)
@lesson.goals << Goal.new(goals)
@lesson.assign_attributes(search_params)
else
@lesson …Run Code Online (Sandbox Code Playgroud)