Ruby on rails 运行rails控制台时出现ActiveRecord错误
当我启动rails控制台并想要测试我刚刚创建的模型模板时,rails控制台无法创建我的新模型实例Ruby on rails 运行rails控制台时出现ActiveRecord错误,ruby-on-rails,ruby,activerecord,ruby-on-rails-5,rails-postgresql,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 5,Rails Postgresql,当我启动rails控制台并想要测试我刚刚创建的模型模板时,rails控制台无法创建我的新模型实例 yellow:maw thierry$ rails console Running via Spring preloader in process 17717 Loading development environment (Rails 5.1.0) >> Account.create(name: "John Doe", email: "john.doe@example.com", pa
yellow:maw thierry$ rails console
Running via Spring preloader in process 17717
Loading development environment (Rails 5.1.0)
>> Account.create(name: "John Doe", email: "john.doe@example.com", password: "1234ABCD", password_confirmation: "1234ABCD")
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "accounts" does not exist
LINE 8: WHERE a.attrelid = '"accounts"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
c.collname, col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = '"accounts"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from (irb):1
>>
正如您所看到的,我已经执行了两次请求:
一个是在生成的模型名称中使用单引号+双引号。
只有单引号的。
有人遇到过这个错误吗?你是怎么解决的
提前感谢您的帮助
背景:
Rails 5.1.0
马科斯塞拉酒店
使用Rbenv。
使用Ruby 2.4.1p111。
PG适配器:0.20.0
在模型目录中:
yellow:maw thierry$ ls -al app/models/
total 24
drwxr-xr-x 6 thierry staff 204 7 mai 14:08 .
drwxr-xr-x 10 thierry staff 340 5 mai 16:23 ..
-rw-r--r--@ 1 thierry staff 531 8 mai 12:44 account.rb
-rw-r--r-- 1 thierry staff 78 5 mai 16:23 application_record.rb
drwxr-xr-x 3 thierry staff 102 5 mai 16:23 concerns
-rw-r--r--@ 1 thierry staff 161 7 mai 18:41 enterprise.rb
yellow:maw thierry$
account.rb的内容:
yellow:maw thierry$ cat app/models/account.rb
class Account < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 250 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
#This validation must be put after has_secure_password in order for the password field to be defined.
validates :password, presence: true, length: { minimum: 6 }
end
yellow:maw thierry$
正如@Iceman在他的评论中所暗示的,这与春天有关。因此,我: 停止轨道控制台。 用弹簧挡块停止弹簧。 用ps aux | grep spring检查弹簧是否真的停止,结果是。 最后重启rails控制台。 结果是:
yellow:maw thierry$ rails console
Running via Spring preloader in process 22940
Loading development environment (Rails 5.1.0)
>> Account.create(name: "John Doe", email: "john.doe@example.com", password: "1234ABCD", password_confirmation: "1234ABCD")
(0.1ms) BEGIN
Account Exists (61.2ms) SELECT 1 AS one FROM "accounts" WHERE LOWER("accounts"."email") = LOWER($1) LIMIT $2 [["email", "john.doe@example.com"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "accounts" ("name", "email", "created_at", "updated_at", "password_digest") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "John Doe"], ["email", "john.doe@example.com"], ["created_at", "2017-05-09 08:14:29.093633"], ["updated_at", "2017-05-09 08:14:29.093633"], ["password_digest", "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5TjUxnkPSzu"]]
(40.5ms) COMMIT
=> #<Account id: 1, name: "John Doe", email: "john.doe@example.com", created_at: "2017-05-09 08:14:29", updated_at: "2017-05-09 08:14:29", password_digest: "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5...">
>>
你重启rails控制台了吗?是的,很多次了。这可能是spring的问题吗?是的!谢谢@IcemanNot当spring第一次遇到困难时,我很难过地放弃了使用它,原因如下。
yellow:maw thierry$ cat app/models/account.rb
class Account < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 250 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
#This validation must be put after has_secure_password in order for the password field to be defined.
validates :password, presence: true, length: { minimum: 6 }
end
yellow:maw thierry$
yellow:maw thierry$ rails console
Running via Spring preloader in process 22940
Loading development environment (Rails 5.1.0)
>> Account.create(name: "John Doe", email: "john.doe@example.com", password: "1234ABCD", password_confirmation: "1234ABCD")
(0.1ms) BEGIN
Account Exists (61.2ms) SELECT 1 AS one FROM "accounts" WHERE LOWER("accounts"."email") = LOWER($1) LIMIT $2 [["email", "john.doe@example.com"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "accounts" ("name", "email", "created_at", "updated_at", "password_digest") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "John Doe"], ["email", "john.doe@example.com"], ["created_at", "2017-05-09 08:14:29.093633"], ["updated_at", "2017-05-09 08:14:29.093633"], ["password_digest", "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5TjUxnkPSzu"]]
(40.5ms) COMMIT
=> #<Account id: 1, name: "John Doe", email: "john.doe@example.com", created_at: "2017-05-09 08:14:29", updated_at: "2017-05-09 08:14:29", password_digest: "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5...">
>>