我有一个需要一些连接/自定义查询的关联.当试图找出如何实现这一点时,重复的响应是finder_sql.但是在Rails 4.2(及更高版本)中:
ArgumentError:未知密钥:: finder_sql
我进行连接的查询如下所示:
'SELECT DISTINCT "tags".*' \
' FROM "tags"' \
' JOIN "articles_tags" ON "articles_tags"."tag_id" = "tags"."id"' \
' JOIN "articles" ON "article_tags"."article_id" = "articles"."id"' \
' WHERE articles"."user_id" = #{id}'
Run Code Online (Sandbox Code Playgroud)
我知道这可以通过以下方式实现:
has_many :tags, through: :articles
Run Code Online (Sandbox Code Playgroud)
但是,如果连接的基数很大(即用户有数千篇文章 - 但系统只有几个标签),则需要加载所有文章/标签:
SELECT * FROM articles WHERE user_id IN (1,2,...)
SELECT * FROM article_tags WHERE article_id IN (1,2,3...) -- a lot
SELECT * FROM tags WHERE id IN (1,2,3) -- a few
Run Code Online (Sandbox Code Playgroud)
当然也对一般情况感到好奇.
注意:也试过使用proc语法,但似乎无法弄清楚:
has_many :tags, -> (user) …Run Code Online (Sandbox Code Playgroud) 我在Ruby中有很多时间范围:
period = Time.parse('8:00am')..Time.parse('8:00pm')
incidents = [
Time.parse('7:00am')..Time.parse('9:00am'),
Time.parse('1:00pm')..Time.parse('3:00pm'),
Time.parse('1:30pm')..Time.parse('3:30pm'),
Time.parse('7:00pm')..Time.parse('9:00pm'),
]
Run Code Online (Sandbox Code Playgroud)
我正试图在这段时间内获得一系列事件免费块.对于上面那个是:
[
Time.parse('9:00am')..Time.parse('1:00pm')
Time.parse('3:30pm')..Time.parse('7:00pm')
]
Run Code Online (Sandbox Code Playgroud)
从上面可以看出事件在这段时间之外重叠或延伸.范围或类似的任何操作是否会使这种计算更简单?
是否有任何标准方法将Postgres范围类型与Rails表单助手集成?我基本上需要一个最小和最大字段,可以在保存时转换为范围.有任何想法吗?
我有一个简单的指令:
HTML:
<div id="app">
<div v-sample:callback="greet"></div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
files: [],
},
methods: {
greet: function (arg) {
alert(arg);
},
},
});
</script>
Run Code Online (Sandbox Code Playgroud)
JS:
Vue.directive('sample', {
bind: function (el, binding, vnode) {
el.addEventListener('...', function () {
// ...
callback.call(arg, ...);
});
},
});
Run Code Online (Sandbox Code Playgroud)
但是,我尚不清楚获取函数和评估的适当语法。从指令中执行此操作的正确方法是什么?
我目前有一个全新的ruby/rails应用程序设置与webpacker.每次部署到Heroku都需要额外的30秒以重新安装纱线依赖(运行yarn install).
remote: [1/4] Resolving packages...
remote: [2/4] Fetching packages...
remote: info fsevents@1.1.3: The platform "linux" is incompatible with this module.
remote: info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
remote: [3/4] Linking dependencies...
remote: warning "babel-loader@7.1.2" has incorrect peer dependency "babel-core@6 || 7 || ^7.0.0-alpha || ^7.0.0-beta || ^7.0.0-rc".
remote: warning "babel-loader@7.1.2" has incorrect peer dependency "webpack@2 || 3".
remote: warning "compression-webpack-plugin@1.1.2" has incorrect peer dependency "webpack@^2.0.0 || ^3.0.0".
remote: warning …Run Code Online (Sandbox Code Playgroud) 我正在创建一个NSDate将a转换NSDate为a 的类别NSString.它使用一个NSDateFormatter来做.我发现每次分配然后释放格式化程序会导致我的应用程序出现明显的延迟(这个类别经常使用),所以我更新了我的'format'方法,如下所示:
- (NSString *)pretty
{
static NSDateFormatter *formatter = nil;
if (formatter == nil)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
}
return [formatter stringFromDate:self];
}
Run Code Online (Sandbox Code Playgroud)
这是在Cocoa中处理静态变量的正确方法吗?这是泄漏(没有dealloc之后alloc)?做这样的事情会有更好的方法吗?谢谢!
如何为隐藏UITabBar的时间添加观察者(通过'hides-bottom-bar-when-pushed')?我的标签栏下方有一个自定义按钮,我想确保UITabBar隐藏时它不会出现。谢谢!
我有多ActiveRecord个子类实例数组Item,我需要根据最早的事件循环打印.在这种情况下,我需要打印打印付款和维护日期如下:
项目5天内需要维护
项目B 6天内
需要付款
项目7天内需要付款项目B维护需要8天
我目前有两个查询maintenance和payment项目查询(非独占查询)和以下输出它们:
<%- item_p = nil -%>
<%- item_m = nil -%>
<%- loop do -%>
<% item_p ||= @items_p.shift %>
<% item_m ||= @items_m.shift %>
<%- if item_p.nil? and item_m.nil? then break -%>
<%- elsif item_p and (item_m.nil? or item_p.paymt < item_m.maint) then -%>
<%= item_p.name %> payment required in ...
<%- elsif item_m and (item_p.nil? or item_m.maint < item_p.paymt) then -%>
<%= item_m.name %> maintenance …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 my 中使用created_at和,但它们似乎没有被设置。知道它们什么时候设置吗?谢谢!updated_atbefore_create
我正在尝试根据整数评级创建一个包含(unicode)'明星'的字符串.我目前有:
NSMutableString *stars = [NSMutabelString stars];
for (int i = 0; i < rating; i++)
{
[stars appendString:@"\u2605"];
}
Run Code Online (Sandbox Code Playgroud)
但是,我发现这有点难看.在不使用此循环方法的情况下,是否存在构造此类字符串的方法?使用字符串格式的东西?
我正在创建一个需要在多个数据库下运行的应用程序.我目前在迁移中有一些代码,我只想在特定的数据库(postgresql和mysql)下运行.有什么方法可以设置吗?谢谢.
ruby ×4
iphone ×3
objective-c ×3
cocoa-touch ×2
postgresql ×2
activerecord ×1
algorithm ×1
cocoa ×1
heroku ×1
ios ×1
javascript ×1
macos ×1
migration ×1
mysql ×1
refactoring ×1
vue.js ×1