我正在实现跟踪用户阅读的文章的功能.
create_table "article", :force => true do |t|
t.string "title"
t.text "content"
end
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的迁移:
create_table :user_views do |t|
t.integer :user_id
t.integer :article_id
end
Run Code Online (Sandbox Code Playgroud)
始终会查询user_views表以查找两列,而不仅仅查找一列.我的问题是我的索引应该是什么样子.这些表的顺序是否存在差异,是否应该有更多选项或其他.我的目标数据库是Postgres.
add_index(:user_views, [:article_id, :user_id])
Run Code Online (Sandbox Code Playgroud)
谢谢.
更新:
因为只有一行在两列中都包含相同的值(因为在知道user_id是否读过article_id时),我应该考虑:unique选项吗?如果我没有弄错,那意味着我不必自己进行任何检查,只需在用户访问文章时插入即可.
当我最近将 Ng 代码库移植到 Nx 12.x 时,我对这条规则感到非常困惑。我希望这篇文章可以帮助其他开始从 Ng 迁移到 Nx 的人。
上面的代码库是一个相当小的单个存储库,现在已在生产中使用。使用 Nx 时,最好遵循 monorepo 的建议,以便将来随着代码库的增长能够使用 monorepo 的优势。(例如,这里我避免过度暴露当前存储库中的代码)。
我将上面的代码库放入my-org/apps/my-small-repo. 通过 linting,我对规则的失败感到困惑@nrwl/nx/enforce-module-boundaries。因此,我尝试了不同的可能性来映射编译器或 linter 或两者都失败的地方src/app。my-org/apps/my-small-repo
我想出了以下解决方案。
就放
"compilerOptions": {
"baseUrl": "src"
},
Run Code Online (Sandbox Code Playgroud)
进入 的根目录apps/my-small-repo/tsconfig.json,并将 中的所有导入替换apps/my-small-repo为以 开头的导入app。
示例DashboardComponent:
import { DashboardComponent } from 'app/components/dashboard/dashboard.component';
Run Code Online (Sandbox Code Playgroud)
该解决方案在 nx 13.x 上进行了测试,但它可能也适用于以前版本的 nx。
放
import { DashboardComponent } from 'app/components/dashboard/dashboard.component';
Run Code Online (Sandbox Code Playgroud)
到您的存储库根paths目录中。然后将规则放入存储库根目录中。compilerOptionstsconfig.base.json"allowCircularSelfDependency": true,@nrwl/nx/enforce-module-boundaries
我们决定"allowCircularSelfDependency": …
module X
end
module Y
end
module Z
#TODO include X replacement of including Y
#TODO include Y replacement of including X
end
Run Code Online (Sandbox Code Playgroud)
有没有办法解决ruby不包含uninclude关键字的事实?

我正在做一个反射游戏.这是输出 -
__________________________________________________________________________
Reflex game initiated. Press ENTER to begin the game, and then press ENTER
after the asterisks are printed to measure your reflexes!.
*************************
Your result: 0.285606 seconds.
logout
[Process completed]
__________________________________________________________________________
Run Code Online (Sandbox Code Playgroud)
但是有一个小问题 - 按下Enter键开始游戏之后和星星打印之前有0-10秒(基于随机变量).在此期间,如果玩家按下ENTER,则将其记录为反射时间.所以我需要一种方法来阻止我的代码在打印星星之前读取他们的ENTER按钮.代码 -
#!/usr/bin/perl
use Time::HiRes qw(sleep);
use Time::HiRes qw(gettimeofday);
#random delay variable
$random_number = rand();
print "Reflex game initiated. Press ENTER to begin the game, and then press ENTER after the asterisks are printed to measure your reflexes!.\n";
#begin button
$begin = <>;
#waits …Run Code Online (Sandbox Code Playgroud) 我想设置三个容器,一个用于logstash,一个用于elasticsearch,一个用于kibana。最后两个都很好,但我需要配置第一个,以便它具有并使用 http 输入插件,然后使用我将传递它的 CSV。
到目前为止,我已经尝试过了,它可以运行,但我认为它没有使用我告诉它的配置
version: '3.3'
services:
logstash:
image: docker.elastic.co/logstash/logstash:6.7.0
# configs:
# - source: logstash_config
# target: /etc/logstash/conf.d/logstash.conf
# command: bash -c "logstash -f /etc/logstash/conf.d/logstash.conf && bin/logstash-plugin install logstash-input-http"
command: bash -c 'bin/logstash -e "input { http { } } output { stdout { codec => rubydebug} }" && bin/logstash-plugin install logstash-input-http'
links:
- elasticsearch
ports:
- 5044:5044
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200" …Run Code Online (Sandbox Code Playgroud) 使用 ActiveStorage 时,如何为附加文件创建范围。
例如:
class Check < ActiveRecord::Base
has_one_attached :image
end
Run Code Online (Sandbox Code Playgroud)
我想要Check.has_attached_image只返回存在附加图像的记录。
我知道这ActiveStorage提供了一个with_attached_image范围。但这似乎不起作用:
irb(main):009:0> Check.with_attached_image.to_sql
=> "SELECT \"checks\".* FROM \"checks\""
对于以下SQL查询:
SELECT COUNT (distinct first_name) from actor;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
"SELECT" is not valid at this position for this server version, expecting: '(', WITH
Run Code Online (Sandbox Code Playgroud)
我是SQL的新手。如何解决此错误?
我将完全相同的行放在具有完全相同模式的另一台PC上,并且工作正常。
我有一个全新的 rails 6 应用程序,我安装了 rspec。
我创建了一个控制器,当我运行 rspec 时出现此错误:
Run Code Online (Sandbox Code Playgroud)PagesController GET #index returns http success Failure/Error: get :index ActionView::Template::Error: wrong number of arguments (given 2, expected 1) # ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>' # ------------------ # --- Caused by: --- # ArgumentError: # wrong number of arguments (given 2, expected 1) # ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
生成的 rspec 测试如下所示:
require 'rails_helper'
RSpec.describe CartController, type: :controller do
describe "GET #index" do
it "returns http success" do
get …Run Code Online (Sandbox Code Playgroud) 我的任务是为使用Angular开发的聊天应用程序编写测试.下面是Angular模板代码片段,我目前正在为以下代码编写测试:
<div class="title-menu-container" fxLayoutAlign="center center">
<button id="save-title-button" mat-icon-button *ngIf="titleInputEdit; else settings">
<mat-icon class="secondary-text" (click)="saveTitle(titleInput.value)">check</mat-icon>
</button>
<ng-template #settings>
<button mat-icon-button [matMenuTriggerFor]="menu" [disabled]="!(isGroupConversation$ | async)">
<mat-icon class="secondary-text">settings</mat-icon>
</button>
</ng-template>
</div>
Run Code Online (Sandbox Code Playgroud)
实际上,如果组件布尔变量'titleInputEdit'为true,则显示save-title-button,否则显示设置按钮.以下是导致问题的测试用例:
it('save title button should be present', () => {
component.titleInputEdit = true;
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('#save-title-button')).not.toBe(null);
});
Run Code Online (Sandbox Code Playgroud)
我只是"模拟"组件变量,调用.detectChanges(),然后测试是否存在按钮.但是,测试失败并显示"预期的空值不为空".
通过各种console.log调用,我已经确认component.titleInputEdit正确设置为true但fixture.nativeElement不包含正确的按钮.
我注意到的一些事情:
如果我将'component.titleInputEdit = true'行移动到我的beforeEach中并将其删除,并从我的测试中调用detectChanges(),则测试通过.
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.titleInputEdit = true
fixture.detectChanges();
debugElement = fixture.debugElement;
});
it('save title button should be present', () => {
expect(fixture.nativeElement.querySelector('#save-title-button')).not.toBe(null);
});
Run Code Online (Sandbox Code Playgroud)如果我从beforeEach()中删除.detectChanges()调用,并将其保留在测试用例中,则测试通过.
我对Angular比较陌生,但我发现有类似问题的人的情况.在尝试了那些帖子中推荐的一些东西后,我仍然在摸不着头脑.更奇怪的是,我已经为其他Angular组件编写了测试,这些组件几乎完全相同,没有任何问题. …
最近我一直在研究一个代码,并因为这个错误而卡住了好几天。基本上,该程序会计算您每天必须摄入多少卡路里。我必须从条目中获取输入,但我不知道如何将该输入(默认为字符串)转换为浮点数以开始使用数字。我在 Tkinter 中使用 Python 3。
这是代码:
from tkinter import *
root = Tk()
root.geometry("1000x500")
root.resizable(FALSE, FALSE)
root.title("BMI Calculator")
def calc(args):
def BMI_temp(args):
print(str(boyage))
BMI = IntVar()
BMI = 66.5 + (13.75 * float(boykg)) + (5.003 * float(boycm)) - (6.755 * float(boyage))
bmi_temp = Label(root, text="This is how many calories you have to eat if you have a non-active life: " + str(float(BMI)))
bmi_temp.grid(row=3, sticky=W)
def boy_age_fnct(args):
boy_age_entry.focus_set()
boy_cm_entry.delete(0, "end")
boy_age.grid(row=2, sticky=W)
boy_age_entry.grid(row=2, column=1)
boy_age_entry.bind("<Return>", BMI_temp)
def boy_cm_fnct(args):
boy_cm_entry.focus_set()
boy_kg_entry.delete(0, "end") …Run Code Online (Sandbox Code Playgroud)