使用rails 3样式我怎么写相反的:
Foo.includes(:bar).where(:bars=>{:id=>nil})
Run Code Online (Sandbox Code Playgroud)
我想找到id为非null的地方.我试过了:
Foo.includes(:bar).where(:bars=>{:id=>!nil}).to_sql
Run Code Online (Sandbox Code Playgroud)
但那回归:
=> "SELECT \"foos\".* FROM \"foos\" WHERE (\"bars\".\"id\" = 1)"
Run Code Online (Sandbox Code Playgroud)
这绝对不是我需要的,而且几乎看起来像是ARel中的一个错误.
我有一个has_many接受嵌套属性的关联.我需要在集合中至少有一个关联对象,所以我写了一个自定义验证器:
class MinimumCollectionSizeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.size < options[:size]
record.errors[attribute] << (options[:message] || "must have at least #{options[:size]} line.")
end
end
end
Run Code Online (Sandbox Code Playgroud)
该模型看起来像:
has_many :foos, :dependent=>:destroy
accepts_nested_attributes_for :foos
validates :foos, :minimum_collection_size=>{:size=>1}
Run Code Online (Sandbox Code Playgroud)
这在模型创建方面效果很好,但在更新时失败了. @my_model.update_attributes(params[:my_model])即使所有foos都被_destroy删除,也会返回true.
我怎么update_attributes能表现得一样save?
这个问题可能是一个长镜头.在删除实体后保存时,我无法弄清楚我在核心数据项目中遇到的错误.我的.xcdatamodel可以在:
我有两个与我合作的主要实体,一件衣服和一件物品.我可以毫无问题地创建它们但是当我删除它们时,我得到以下错误日志:
为装备:
2009-09-22 20:17:37.771 itryiton[29027:20b] Operation could not be completed. (Cocoa error 1600.)
2009-09-22 20:17:37.773 itryiton[29027:20b] {
NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1600.)";
NSValidationErrorKey = outfitArticleViewProperties;
NSValidationErrorObject = <Article: 0x12aa3c0> (entity: Article; id: 0x12b49a0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/Article/p1> ; data: {
articleID = 2009-09-22 19:05:19 -0400;
articleImage = 0x12b4de0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/ArticleImage/p1>;
articleType = nil;
attributeTitles = "(...not nil..)";
color = nil;
comment = nil;
dateCreated = 2009-09-22 19:05:19 -0400;
designer = nil;
imageView = "(...not nil..)";
location = …Run Code Online (Sandbox Code Playgroud) 我正在从rails 2.3.8升级到3.0.0,所以我需要替换remote_form_forhelper调用form_for(@object, :remote=>true).
我一直在跟随Simone Carletti,但我似乎无法让ajax回调rails.js起火.
我生成的HTML是:
<form accept-charset="UTF-8" action="/vendor_shipments" class="new_vendor_shipment" data-remote="true" id="formname" method="post">
Run Code Online (Sandbox Code Playgroud)
我正在测试的javascript:
jQuery(function($){
alert('document ready');
$("#formname")
.bind('ajax:loading', function() {alert("loading!");})
.bind('ajax:success', function(data, status, xhr) {alert("success!");})
.bind('ajax:failure', function(xhr, status, error) {alert("failure!");})
.bind('ajax:complete', function() {alert("complete!");});
});
Run Code Online (Sandbox Code Playgroud)
触发'文档就绪'警报,并且成功执行了ajax请求(数据被发布到服务器),但没有任何' ajax:____'回调触发.
我究竟做错了什么?
(对于它的价值,表单本身是通过ajax加载的)
在提交时,我不小心包含了密码.如何从存储库中删除此提交?
推送到了github,所以提交从历史中完全删除是很重要的.
我明白如何将:controller,:action, :etc为URL.我正在寻找相反的方法,如何从URL中找到rails路由器将调用的操作?
我在模块中动态定义一个方法,并且我想检查一旦该方法绑定到一个类实例,该方法的主体就是我所期待的.有没有办法输出(作为文本)方法的主体?
模块controller_mixins.rb:
module ControllerMixin
instance_eval "def search_by_vendor (*args) \n" \
" @#{self.class.name.sub(/Controller/, '').tableize} = #{self.class.name.sub(/Controller/, '')}.find_all_by_vendor_id(params[:vendor_id]) \n"\
"respond_to do |format| \n" \
" format.html { render :template=>'/#{self.class.name.sub(/Controller/, '').tableize}/index', :layout=>'vendor_info'} \n" \
" format.xml { render :xml => @#{self.class.name.sub(/Controller/, '').tableize} } \n" \
"end \n"\
"end \n"
end
Run Code Online (Sandbox Code Playgroud)
上课与:
class VendorOrdersController < ApplicationController
# GET /vendor_orders
# GET /vendor_orders.xml
require 'controller_mixins'
include ControllerMixin
<rest of class>
Run Code Online (Sandbox Code Playgroud)
因此VendorOrdersController
,script/console为了方便起见,我希望看到mixin的实现.
更新:Per @~ /我将字符串保存到变量并且puts'd it.这非常有效.这揭示了我的代码中的错误(我想首先看到代码的原因).下面的代码要好得多,并按预期工作.
module ControllerMixin …Run Code Online (Sandbox Code Playgroud) 我有两个子类,一个有很多自定义,我们称之为Foo,另一个子类只需要重写一个方法,并且不需要任何其他变量,我们称之为Bar.
Bar将是Foo的变量之一,所以为了避免再使用2个文件(.m和.h for Bar)我想在Foo的.h和.m文件中实现Bar的接口和实现.
我的最大努力给了我几个编译器错误.
.h文件看起来像:
#import <UIKit/UIKit.h>
@interface Foo : FooSuperClass {
Bar *barVariable;
}
@property (nonatomic, retain) Bar *barVariable;
-(void) fooMethod;
@end
@interface Bar : BarSuperClass {
}
@end
Run Code Online (Sandbox Code Playgroud)
.m文件看起来像:
#import "Foo.h"
@implementation Foo
@synthesize barVariable;
-(void) fooMethod{
//do foo related things
}
@end
@implementation Bar
- (void)barSuperClassMethodIWantToOverride{
}
@end
Run Code Online (Sandbox Code Playgroud)
我意识到这类事情通常会受到不满,但我觉得这在我的情况下是合适的.我得到的第一个错误是"Bar之前的预期说明符 - 限定符列表".
我做错了什么,我很确定在单个文件中可以有多个声明/定义.
我熟悉使用NSLocalizedString()来本地化字符串,但我今天遇到的问题需要更多的技巧.我的情况是这样的:
NSString *userName; //the users name, entered by the user. Does not need localized
NSString *favoriteFood; //the users favorite food, also entered by user, and not needing localized
NSString *summary = [NSString stringWithFormat:@"%@'s favorite food is %@", userName, favoriteFood];
Run Code Online (Sandbox Code Playgroud)
这适用于英语,但并非每种语言都使用与英语相同的单词排序,例如,从日语到英语的同一个语言的逐字翻译将为:
UserName最喜欢的食物披萨是
更不用说这并不是每种语言都占有一席之地.
有哪些技术可用于本地化这种连锁句子?
更新其他人的好处:@Jon Reed是对的,位置说明者对本地化非常重要.他链接的文档只包含对它们可以与NSString,NSLog和其他人一起使用的事实的引用,该链接并不真正告诉我们如何使用它们.
我找到了这个链接,这很好地解释了.它也比我更好地解释了我的问题.从链接:
printf和sprintf的格式字符串(参见Printf)给翻译带来了特殊问题.请考虑以下事项:1
Run Code Online (Sandbox Code Playgroud)printf(_"String `%s' has %d characters\n", string, length(string))) A possible German这个翻译可能是:
Run Code Online (Sandbox Code Playgroud)"%d Zeichen lang ist die Zeichenkette `%s'\n" The problem应该是显而易见的:格式规范的顺序与原来不同!尽管gettext可以在运行时返回已翻译的字符串,但它无法在调用printf时更改参数顺序.
要解决这个问题,printf格式说明符可能有一个额外的可选元素,我们称之为位置说明符.例如:
Run Code Online (Sandbox Code Playgroud)"%2$d Zeichen lang ist die Zeichenkette `%1$s'\n" Here, the …
我正在阅读jQuery UI源代码(具体是ui-dialog),我看到这个模式重复多次:
var self = this,
options = self.options,
uiDialog = self.uiDialog;
Run Code Online (Sandbox Code Playgroud)
这种模式背后的原因是什么, var self = this, something, something else
jquery ×2
objective-c ×2
ajax ×1
arel ×1
cocoa ×1
core-data ×1
git ×1
github ×1
javascript ×1
jquery-ui ×1
localization ×1
routing ×1
ruby ×1