我可以使用 ActiveRecord 获得以下 SQL 查询的相同输出吗?
SELECT d.title, COUNT(ep.*)
FROM posts d
LEFT JOIN comments ep
ON d.title = ep.post_title
GROUP BY d.title;
我试过Post.joins(:comments).group(:title).count('comments.*')
。
我可以使用 ActiveRecord 获得以下 SQL 查询的相同输出吗?
SELECT d.title, COUNT(ep.*)
FROM posts d
LEFT JOIN comments ep
ON d.title = ep.post_title
GROUP BY d.title;
我试过Post.joins(:comments).group(:title).count('comments.*')
。
我已经下载了最新的(v4.0.4)TailwindCSS 独立 CLI 并创建了以下文件。
tailwind.config.js
module.exports = {
content: ["./index.html"],
theme: {
extend: {},
},
plugins: [],
}
tailwind.css
@tailwind base;
@tailwind components;
@tailwind utilities;
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<p class="text-emerald-400 text-5xl font-bold">Lorem Ipsum</p>
</body>
</html>
我开始用
./tailwindcss -i tailwind.css -o style.css --watch
≈ tailwindcss v4.0.4
Done in 41ms
但是,style.css
我的页面中不包含样式index.html
。
样式.css
/*! tailwindcss v4.0.4 | MIT License | https://tailwindcss.com */
.relative {
position: relative;
}
.mx-auto {
margin-inline: auto;
}
.flex {
display: flex;
}
.inline-flex {
display: inline-flex;
}
.w-full {
width: 100%;
}
.flex-col {
flex-direction: column;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
.justify-end {
justify-content: flex-end;
}
.justify-start {
justify-content: flex-start;
}
.object-cover {
object-fit: cover;
}
.text-center {
text-align: center;
}
.transition-all {
transition-property: all;
transition-timing-function: var(--tw-ease, ease);
transition-duration: var(--tw-duration, 0s);
}
.duration-700 {
--tw-duration: 700ms;
transition-duration: 700ms;
}
@property --tw-duration {
syntax: "*";
inherits: false;
}
我在这里遗漏了什么?
我需要从 Rails 应用程序中包含的模块访问 ActiveRecord 常量。
class User < ApplicationRecord
include ModOne
OPTIONS = {a: 1}.freeze
...
end
Module ModOne
extend ActiveSupport::Concern
included do
do_semething(self::OPTIONS)
end
class_methods do
def do_something(opts)
...
end
end
end
但我明白
NameError: 未初始化常量 User(调用“User.connection”建立连接)::OPTIONS 您是指?User::OPTIONS
我在这里遗漏了什么?
我也尝试过self
用base_class
和事件来替换User
但是出现了同样的错误。
这几天我一直在试图解决这个问题,但我无法让它发挥作用。我已经在 Stack-overflow 上查看了很多问题,但仍然无法找出解决它的正确方法。
我有表示带有数字和“多字”变量的算术表达式的字符串,例如(Car speed / Road-congestion Time) + 100
和 我需要匹配除 之外的所有内容+-*/()
。
我需要获得以下三场比赛:
以下表达式类型适用[+-\\*\\/()]?([^+-\\*\\/()]*)[+-\\*\\/()]?
于“多单词”变量中没有连字符的字符串(它在末尾添加了一个额外的空匹配)。为了区分减号和连字符,减号总是被空格包围,但我不知道如何在正则表达式中指定它。
有什么想法如何更新我的正则表达式以适应所有情况吗?另外,正则表达式应该忽略数字(仅获取上例中的前两个匹配项)。
PS - 拆分是最后一个选项,如果可能的话我想使用正则表达式。
在 Rails 7 中,为什么以下助手不渲染列表项?
def list(options)
tag.ul(class: "a") do
options.each do |option|
tag.li(class: "b") do
tag.span(option, class: "c")
end
end
end
end
我打电话<%= list(["X", "Y", "Z"]) %>
,渲染的很简单 <ul class="a"></ul>
。
我在这里缺少什么?
在 Ruby 中,我需要检查数组的所有元素是否在方向上交替,即前驱和后继是否大于或小于每个元素。
给出以下数组,结果必须如所示
[1,3,2,4,3] # => true
[3,2,4,3,5] # => true
[1,2,3,1,3]. # => false
[1,2,2,1,3]. # => false
我得出了以下代码,它似乎有效,但它很漂亮或易于理解。
array.each_cons(2) # get subsequent couple of items
.map {|a,b| b-a}. # get the difference between first and second
.map {|n| n <=> 0} # get the sign (-1/+1)
.each_cons(2) # get couple of signs
.map {|a,b| a+b} # sum each couple
.uniq == [0] # return true if all the couple are 0
有什么建议如何简化检查吗?