我正在尝试使用柴油将 postgres 数据库添加到火箭应用程序。我的main.rs文件看起来像这样,但给出了错误“该特征diesel::Connection未实现DbConnection”.get_result(connection)
#[macro_use] extern crate diesel;
extern crate dotenv;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
use diesel::prelude::*;
use rocket_contrib::database;
use rocket_contrib::json::JsonValue;
mod models;
mod schema;
use self::models::*;
use self::schema::*;
#[database("my_db")]
struct DbConnection(diesel::PgConnection);
#[get("/")]
fn index(connection: DbConnection) -> JsonValue {
json!(all_bicycles(&connection))
}
fn create_bicycle<'a>(connection: &DbConnection, make: &'a str, model: &'a str, rider_type: &'a str, size: &'a str) -> Bicycle {
let new_bicycle = NewBicycle {
make,
model,
rider_type,
size
};
diesel::insert_into(bicycles::table) …Run Code Online (Sandbox Code Playgroud) enum在 Rails 和 ActiveRecord 中,我正在努力解决模型中的行为。首先,default当我运行测试时,我在 AR 迁移中在数据库级别设置的 没有生效,其次,我根本无法更新它。这是一些代码,为了简洁起见,进行了简化。
在我的模型中,枚举声明如下:
class MyModel < ApplicationRecord
# as you can see this is a join table for different_model and another_model
belongs_to :different_model
belongs_to :another_model
enum blocked: %w[no yes] # also tried with symbols, like: %i[no yes]
end
Run Code Online (Sandbox Code Playgroud)
迁移如下:
class AddBlockedToMyModel < ActiveRecord::Migration[5.2]
def change
add_column :my_models, :blocked, :integer, default: 0
end
end
Run Code Online (Sandbox Code Playgroud)
我使用以下方法创建对象:
my_model = MyModel.where(different_model: @different_model, another_model: @another_model).first_or_create
Run Code Online (Sandbox Code Playgroud)
这有效,我曾经byebug检查过该记录是否持久存在(因此也有效)
然后尝试更新blocked它的枚举:
my_model.update(blocked: 'yes')
Run Code Online (Sandbox Code Playgroud)
但blocked属性仍保持为 …
我一直收到这个错误:
未初始化的常量GetxmlController :: Xmlparse
我的类代码看起来像
require "rexml/document"
include REXML
Class Xmlparse
def parsetime
xmlfile = File.new("colors.xml")
xmldoc = Document.new(xmlfile)
root = xmldoc.root
return root
end
end
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像:
class GetxmlController < ApplicationController
def findxml
@hxml=Xmlparse.new
@test1=@hxml.parsetime
end
end
Run Code Online (Sandbox Code Playgroud)
那么我在这里做错了什么?