我在Debian 7下使用rails 4.1.5和postgresql 9.1,我无法在我的开发环境中创建数据库.我跑的时候
bin/rake db:create
Run Code Online (Sandbox Code Playgroud)
我明白了
home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect': FATAL: database "direct-dev" does not exist
Run `$ bin/rake db:create db:migrate` to create your database (ActiveRecord::NoDatabaseError)
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
from ...
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建数据库,因此,它自然不存在.但是rails应该创建它...这是我的config/database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: direct-dev
Run Code Online (Sandbox Code Playgroud)
这是postgresql日志的一部分:
2014-09-01 19:30:40 CEST LOG: connection received: host=[local]
2014-09-01 19:30:40 CEST LOG: connection authorized: user=rs database=direct-dev
2014-09-01 19:30:40 CEST FATAL: database "direct-dev" does not exist
Run Code Online (Sandbox Code Playgroud)
你有什么指针吗?我已经在这一个多小时了,仍然不明白为什么会发生这种情况......
谢谢!
我有三个与has_many相关的模型:通过关联:
class Account < ApplicationRecord
has_many :account_owners
has_many :employees, through: account_owners
def is_owned_or_belongs_to_team_of_employees(employee)
employee.team.any? { |m| employees.include?(m) }
end
end
class AccountOwner < ApplicationRecord
belongs_to :account
belongs_to :employee
end
class Employee < ApplicationRecord
has_many :account_owners
has_many :accounts, through: :account_owners
def team
self.class.where(
'id IN (?)',
self. class.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (
SELECT id, ARRAY[id]
FROM employees
WHERE id = ?
UNION ALL
SELECT employees.id, path || employees.id
FROM search_tree
JOIN employees ON employees.manager_id = search_tree.id
WHERE NOT employees.id = …Run Code Online (Sandbox Code Playgroud) 长期以来,Railsmethod: :post在 helper 中提供了一个选项link_to:当给出该选项时,Rails 会拦截点击并发出 POST 请求,而不是默认的 GET 请求。
然而,由于某种未知的原因,这在 Rails 7 中不起作用:尽管添加method: :post到我的link_to帮助器中,Rails 还是发送了 GET 请求(而不是 POST 请求)。我以为 Turbo 应该解决这个问题,但似乎并没有发生。
您可以执行以下操作来重现,非常简单的步骤:
$ rails new example_app
$ bin/rails g scaffold Book title
$ bin/rails db:create && bin/rails db:migrate
$ echo "<%= link_to "New book", new_book_path, method: :post %>" >> app/views/books/index.html.erb
$ bin/rails s
Run Code Online (Sandbox Code Playgroud)
现在从网络浏览器访问localhost:3000/books,然后单击第二个“新书”链接。我预计会收到错误(毕竟,我没有配置正确的 POST 路由),但不幸的是,Rails 发出 GET 请求 - 而不是 POST 请求,因为它应该有:
Started GET "/books/new" for ::1 …Run Code Online (Sandbox Code Playgroud)