我现在感到困惑,我不知道如何删除/销毁连接表中的记录:
class Task < ActiveRecord::Base
belongs_to :schema
belongs_to :to_do
end
class Todo < ActiveRecord::Base
belongs_to :schema
has_many :tasks
end
class Shcema < AcitveRecord::Base
has_many :todos
has_many :tasks, :through => :todos
end
Run Code Online (Sandbox Code Playgroud)
>> sc = Schema.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
...
>> sc.tasks.delete(Task.first) # I just want to delete/destroy the join item here.
# But that deleted/destroyed the Task.first.
Run Code Online (Sandbox Code Playgroud)
如果我只想破坏关系项,我该怎么办?
假设我有一个用户拥有并属于许多角色的关联.当我销毁用户时,连接表中的记录是否也会被自动删除?或者我需要使用:dependent =>:destroy?如果我摧毁一个角色怎么办?
class User < ActiveRecord::Base
has_and_belong_to_many :roles # need to use :dependent => :destroy to remove join record?
end
class Role < ActiveRecord::Base
has_and_belong_to_many :users # need to use :dependent => :destroy to remove join record?
end
Run Code Online (Sandbox Code Playgroud) 在我的rails应用程序中,子记录得到了marks_for_destruction我想撤消该操作并取消标记它们.有办法吗?
提前致谢.
我有一个我试图销毁的骨干模型,但是没有与请求一起发送参数,因此服务器返回"删除404未找到"错误.
我承认我的结构有点奇怪,因为我根据它们是否已经在列表中创建/销毁这些项目.
var list_item = new MyApp.Models.ListItem({item_id: this.model.id, group_id: this.model.group_id});
if($(e.currentTarget).hasClass('add')){
list_item.save(list_item, {
success: function(response){
this.model.attributes.addedtolist_id = response.id
console.log(this.model);
},
error: function(){
alert('could not save item');
}
});
} else if($(e.currentTarget).hasClass('remove')) {
list_item.id=this.model.addedtolist_id;
list_item.attributes.id = this.model.addedtolist_id;
console.log(list_item);
list_item.destroy({
success: function(){
alert('delete');
},
error: function(){
alert('could not uncheck');
}
});
}
list_item在销毁之前的控制台输出是
_escapedAttributes: Object _previousAttributes: Object _setting: false attributes: Object id: 2 item_id: 66 group_id: 64 __proto__: Object cid: "c23" id: 2 __proto__: q
但是当我查看使用删除请求发送的标头时,我没有发送任何参数.
-----------------------更新params被发送,404仍然被退回--------------
根据Yaroslav的建议,我在destroy方法中添加了一个'header',但我的rails控制器仍然返回DELETE 404 not …
我试图从Devise中覆盖destroy方法SessionsController,但我还没有成功.我已经为该create方法完成了它,但我不知道为什么它不适用于该destroy方法.
这是我的SessionsController:
module Api
module V1
class SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token, if: :json_request?
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
resource.update_token
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true}
end
# DELETE /resource/sign_out
def destroy
puts "DELETE /resource/sign_out"
return render :json => {:success => true}
end
def failure
return render :json …Run Code Online (Sandbox Code Playgroud) 我使用以下代码删除每个视图组上的子项:
protected void onDestroy() {
super.onDestroy();
this.liberarMemoria();
}
public void liberarMemoria(){
imagenes.recycleBitmaps();
this.unbindDrawables(findViewById(R.id.RelativeLayout1));
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
Run Code Online (Sandbox Code Playgroud)
其中视图:R.id.RelativeLayout1是ListView.
但这样做我有例外:
E/AndroidRuntime(582): java.lang.RuntimeException: Unable to destroy activity {...}: java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
关于destroy方法的github页面上有一些讨论,有关堆栈的几个问题,但是还没有一个直截了当的答案或解决方案,我在很多搜索之后能够找到.
当前版本的flexslider http://www.woothemes.com/flexslider/没有destroy方法.在说明中它表示前1.8版本,但使用该方法不起作用.
我需要取消绑定flexslider元素,然后在另一个元素上调用.flexslider(),因为我不希望同时运行多个滑块.
我怎样才能做到这一点?注意:删除导航元素,删除类,解开UL并删除".clone" li是不够的!我想完全将滑块元素恢复到原始状态!
现在我在初始化flexslider之前克隆滑块,然后使用.after()在滑块后插入克隆,然后删除滑块.但这对我来说似乎是一种非常沉重的态度.
$projCur.addClass('flexslider').flexslider({
animation: "slide",
animationSpeed: 500,
slideshow: false,
manualControls: '.dot-nav li a'
});
Run Code Online (Sandbox Code Playgroud)
谢谢!
我相信这是Rails 3中的一个错误.我希望有人能指引我朝着正确的方向前进.下面发布的代码纯粹是为了说明这个问题.希望这不会混淆问题.
鉴于我有一个Post模型和一个Comment模型.发表has_many评论,评论发表于发帖.
在Post模型上设置default_scope,定义join()和where()关系.在这种情况下,()依赖于连接().
通常帖子不依赖于评论.再说一遍,我只想举一个简单的例子.当where()依赖于join()时,这可能是任何情况.
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
default_scope joins(:comments).where("comments.id < 999")
end
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
end
Run Code Online (Sandbox Code Playgroud)
运行以下命令:
Post.update_all(:title => Time.now)
Run Code Online (Sandbox Code Playgroud)
生成以下查询,并最终抛出ActiveRecord :: StatementInvalid:
UPDATE `posts` SET `title` = '2010-10-15 15:59:27' WHERE (comments.id < 999)
Run Code Online (Sandbox Code Playgroud)
同样,update_all,delete_all,destroy_all的行为方式相同.当我的应用程序在尝试更新counter_cache时抱怨时,我发现了这种行为.最终深入研究update_all.
我有一个可调整大小的父div(仅宽度) - 在这个div中我有许多其他div也是可调整大小的(仅限高度).
有时我想要禁用或破坏父宽度调整大小,但保持内部高度调整大小.
当我打电话时$("#idTopDiv").resizable("destroy");,这也会破坏所有子div上的可调整大小.
典型布局是: -
<div id=idDivTop> <!-- Resizable width -->
<div id=idInnerOne>
</div>
<div id=idInnerTwo> <!-- Resizable height -->
<div>
</div>
Run Code Online (Sandbox Code Playgroud)
欣赏任何想法.
我正在为Web和移动开发WebGL应用程序.我经常使用硬刷新来测试我的WebGL实现的结果.视图尝试后,我收到错误:
Error: WebGL: Exceeded 16 live WebGL contexts for this principal, losing the least recently used one.
Run Code Online (Sandbox Code Playgroud)
这不会出现在新启动的浏览器上,而是在多次刷新网站后出现.我想WebGL上下文没有完成,发布,销毁,清理,正确释放.
我怎样才能做到这一点?
Khronos Group在这里创建了一个用于释放和垃圾收集WebGL上下文的测试套件:https://www.khronos.org/registry/webgl/sdk/tests/conformance/context/context-creation-and-destruction.html(注意:这个可能会崩溃您的浏览器!)
测试通过PASS和TEST COMPLETE,所以基本上测试没有检测到任何问题.但是,打开JavaScript控制台,它会读取33个实例:
Error: WebGL: Exceeded 16 live WebGL contexts for this principal, losing the least recently used one.
Run Code Online (Sandbox Code Playgroud)
这是浏览器如何处理WebGL的错误吗?或者我做错了什么?我从没想过要释放任何WebGL上下文.
我正在使用Firefox Developer Edition 48.0a2和Firefox 46.0.1.
如何释放和垃圾收集WebGL上下文?
destroy ×10
join ×2
jquery ×2
activerecord ×1
android ×1
backbone.js ×1
devise ×1
flexslider ×1
free ×1
javascript ×1
json ×1
resizable ×1
slideshow ×1
webgl ×1