有没有办法在控制器中获取URL的锚点部分?
示例:如果我键入http://www.foo.com/bar#anchor123,我可以在控制器中获取字符串anchor123吗?
我如何获得模型的has_many关联?
例如,如果我有这个类:
class A < ActiveRecord::Base
has_many B
has_many C
end
Run Code Online (Sandbox Code Playgroud)
我会这样的方法:
A.get_has_many
Run Code Online (Sandbox Code Playgroud)
那回归
[B,C]
Run Code Online (Sandbox Code Playgroud)
可能吗?谢谢!
在rails 3.2中有一种方法(一个gem,一个插件或其他东西)来知道哪一行代码触发了数据库查询?例如,在我的日志中,我有:
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
Run Code Online (Sandbox Code Playgroud)
我怎么知道触发查询的代码行?谢谢...
我有一个位板,如果只有一位设置为1,我想检查C.
#include <stdint.h>
typedef uint64_t bboard;
bboard b = 0x0000000000000010;
if (only_one_bit_set_to_one (b)) // in this example expected true
// do something...
Run Code Online (Sandbox Code Playgroud)
有没有想过写这个功能int only_one_bit_set_to_one (bboard b)?
我需要将名称范围与运算符组合......类似于:
class Product < ActiveRecord::Base
belongs_to :client
scope :name_a, where("products.name = 'a'")
scope :client_b, joins(:client).where("clients.name = 'b'")
scope :name_a_or_b, name_a.or(client_b)
end
Run Code Online (Sandbox Code Playgroud)
谢谢
我已经开始使用rails 3 beta3了,我发现控制器内的@template变量是零.如何在控制器内调用助手方法?谢谢
我正在编写Ruby扩展并使用该函数Data_wrap_struct.
为了参与Ruby的标记和清除垃圾收集过程,我需要定义一个例程来释放我的结构,以及一个例程来标记从我的结构到其他结构的任何引用.我通过经典free函数释放内存,但我不知道如何使用标记功能.
我的结构听起来像这样
typedef struct
{
int x;
int y;
} A;
typedef struct
{
A collection[10];
int current;
} B;
Run Code Online (Sandbox Code Playgroud)
我认为我需要一个标记函数来标记collection结构B中的引用.
有人可以给我看一个例子来看看标记功能是如何工作的吗?
我有这个架构:帖子belongs_to类别和类别has_many帖子.使用gem globalize3将帖子和类别全球化
class Post < ActiveRecord::Base
belongs_to :category
translates :title, :excerpt, :desc # globalize3
end
class Category < ActiveRecord::Base
has_many :posts
translates :name # globalize3
end
Run Code Online (Sandbox Code Playgroud)
在我的PostsController中,我获得了这行代码的所有帖子:
def index
@posts = Post.includes([:translations, {:category => :translations}])
end
Run Code Online (Sandbox Code Playgroud)
问题是我对类别转换表有n + 1个查询问题:
Post Load (0.3ms) SELECT "posts".* FROM "posts"
Post::Translation Load (0.3ms) SELECT "post_translations".* FROM "post_translations" WHERE ("post_translations".post_id IN (2,3,4))
# START n+1 query block
Category Load (1.9ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" IN (9,12,11))
Category::Translation Load (0.4ms) SELECT "category_translations".* FROM "category_translations" …Run Code Online (Sandbox Code Playgroud) 我有资源订单.在我的管理面板(activeadmin)中,我需要一个布尔过滤器来获取用户具有特定角色的所有订单.
在Order类中,我有一个这样的范围:
class Order
belongs_to :user
scope :client_only_in, -> { joins(:user).where('users.role = ?', 'client') }
end
Run Code Online (Sandbox Code Playgroud)
在活动管理员order.rb我添加了以下过滤器:
filter :client_only, as: :check_boxes
Run Code Online (Sandbox Code Playgroud)
使用rails 3(元搜索),我可以添加
search_method :client_only_in, type: :boolean
Run Code Online (Sandbox Code Playgroud)
得到过滤器,但使用rails 4和Ransack我不知道该怎么做.
如果不添加search_method订单模型,我会收到错误
undefined method `client_only_in' for Ransack::Search
Run Code Online (Sandbox Code Playgroud)
当我访问索引管理员订单页面.
有帮助吗?
我有以下起始代码来获取 X 窗口的屏幕截图(该窗口可以被覆盖、不可见或最小化)。
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xrender.h>
int
main ()
{
Display *display = XOpenDisplay (NULL);
XID xid = 90177543; // xdotool search --name "World of Warcraft" | head -1
// Check if Composite extension is enabled
int event_base_return;
int error_base_return;
if (XCompositeQueryExtension (display, &event_base_return, &error_base_return))
printf ("COMPOSITE IS ENABLED!\n");
// Requests the X server to direct the hierarchy starting at window to off-screen storage
XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);
// Preventing the …Run Code Online (Sandbox Code Playgroud) ruby ×4
activerecord ×3
c ×3
activeadmin ×1
anchor ×1
bitboard ×1
filter ×1
globalize2 ×1
has-many ×1
helpers ×1
include ×1
linux ×1
logging ×1
ransack ×1
scope ×1
screenshot ×1
templates ×1
url ×1
x11 ×1
xlib ×1