首页 \ 问答 \ Rails:从has-many关联中获取所有关联模型(Rails: Get all associated models from has-many association)

Rails:从has-many关联中获取所有关联模型(Rails: Get all associated models from has-many association)

我需要从其他关联模型中获取所有关联模型,我希望首先运行查询。

例如,我有Post模型和Tag模型。 我需要获得与一些标签相关的所有帖子。 没有问题,如果我只有一个标签 - 只需要调用'tag.posts',但如果我有更多,那么一个标签 - 例如,我需要做一些事情,如:

Post.where(id: PostTag.where(tag_id: some_ids).pluck(:category_id).uniq)

我相信Rails有一个内置的解决方案。 那么,谁知道呢?


I need to get all associated models from the other associated model, on which I want to run query first.

For example, I got Post model and Tag model. I need to get all Posts, which associated with some Tags. There's no problem, if I have only one Tag – just call 'tag.posts', but if I have more, then one Tag – for example, I need to do somethink like:

Post.where(id: PostTag.where(tag_id: some_ids).pluck(:category_id).uniq)

I belive that Rails have a built-in solution. So, anybody knows it?


原文:https://stackoverflow.com/questions/16551235
更新时间:2023-12-01 15:12

最满意答案

据我所知,您无法在本地设置编译器选项。 但是,您可以使用诊断编译指示在本地禁用虚假警告。

但是,执行此操作的最佳方法可能是将C99 / C11代码隔离在单独的.c文件中,这些文件与项目的其余部分分开编译,然后将它们全部链接在一起。


As far as I'm aware, you can't set compiler options locally. You can however disable spurious warnings locally, by using diagnostic pragmas.

However, the best way to do this is probably to isolate the C99/C11 code in separate .c files, which you compile separately from the rest of the project, before linking it all together.

相关问答

更多
  • 我认为你会忽略另一种情况,相反的顺序。 考虑一个初始化的int其存储被重用来创建一个std::atomic_int 。 所有原子操作都发生在ctor完成后,因此在初始化的内存中。 但是对于现在被覆盖的int任何并发,非原子访问也必须被禁止。 (我在这里假设存储寿命已经足够,并且不起作用) 我不完全确定,因为我认为第二次访问int将无效,因为访问表达式int的类型与当时的对象类型( std::atomic )不匹配。 但是,“当时的对象类型”假定单线性时间进度不适用于多线程环境。 一般而言,C ++ ...
  • 如果您希望Eclipse使用已安装的devtoolset-2 ,则需要从启用了devtoolset的环境中启动Eclipse。 最明显的是,这可以通过终端来完成 scl enable devtoolset-2 eclipse & 解释: devtoolset作为备用开发环境安装,默认情况下不活动。 只有在显式激活时,您才会获得理解-std=c++11的新编译器版本以及您正在寻找的功能。 If you want Eclipse to work with the installed devtoolset-2, ...
  • 在前面提到的文档中说: http : //clang.llvm.org/docs/UsersManual.html#clang-cl clang默认为Windows目标的C ++ 11。 也许你不需要通过任何标志clang-cl。 In the documentation mentioned before says: http://clang.llvm.org/docs/UsersManual.html#clang-cl clang defaults to C++11 for Windows targets ...
  • 您在CUDA 7.5中不支持您尝试做的事情。 目前,使用icc作为CUDA 7.5中的主机编译器不支持C ++ 11语法,CUDA 7.5不支持icc 16。 CUDA 8.0RC支持使用ICC作为带有std = c ++ 11标志的主机编译器(并且还支持ICC 16.0) What you are trying to do is unsupported in CUDA 7.5. C++11 syntax is not currently supported using icc as the host co ...
  • 由于默认情况下,GCC 5.x版本以-std=gnu11运行,因此Code :: Blocks必须对编译器执行某些操作(例如传递-ansi或-std=gnu90 )以使其工作方式不同。 调查发送给编译器的所有选项。 找到一种方法让Code :: Blocks显示编译时使用的准确咒语。 然后弄清楚如何解决它。 使用的选项是: -Wall -Wextra -Werror -Wstrict-prototypes -Wconversion -std=gnu11 \ -O0 -g -ansi `pkg-config ...
  • 据我所知,您无法在本地设置编译器选项。 但是,您可以使用诊断编译指示在本地禁用虚假警告。 但是,执行此操作的最佳方法可能是将C99 / C11代码隔离在单独的.c文件中,这些文件与项目的其余部分分开编译,然后将它们全部链接在一起。 As far as I'm aware, you can't set compiler options locally. You can however disable spurious warnings locally, by using diagnostic pragmas. ...
  • 代码无效。 看重点: 6.3.2.3(1)指向void的指针可以转换为指向任何对象类型的指针。 指向任何对象类型的指针可以转换为指向void的指针, 然后再返回 ; 结果应该等于原始指针 。 您正在将int**转换为void* (精细),然后转换为不同类型的void** (不正常)。 C不保证您可以安全地将void*转换为除原始类型之外的任何其他内容(除了转换为char * ) 另外, *(void**)dvoidptr导致void* ,而实际上有int* 。 例如,如果sizeof(void*) == 2 ...
  • 最简单的就是推杆 CFLAGS+=" -std=c11" 进入你的configure.ac (除了AC_PROG_CC )。 configure.ac是shell脚本的模板,因此您只需将shell代码放入其中即可。 事实上,所有AC_FOO_BAR m4宏都会自行扩展为shell代码。 警告:这不会检查您的编译器是否实际支持-std=c11标志。 如果要检查,可以使用autoconf存档中的 AX_CHECK_COMPILE_FLAG : AX_CHECK_COMPILE_FLAG([-std=c11], ...
  • 标头是标准C的一部分。但POSIX增加了更多。 由于struct sigevent不是C,但在POSIX -std=c11禁用(可能是ifdef某处)。 gcc test.c 因为gcc默认启用某些级别的POSIX函数和很多GNU扩展,所以可以正常工作。 编译它: gcc -std=c11 -D_POSIX_C_SOURCE=200809 file.c The header is part of standard C. But POSIX adds more t ...
  • 看一开始: '-std=c11', '-x', 'c' '-isystem', 最后两个项目变为c-isystem (参见:YcmDebug输出)。 它可能会混淆编译器。 我想你的意思是-c而不是。 然而,对于YouCompleteMe ,此标志是多余的,可以安全地省略。 PS C ++头文件也会混淆C编译器,因此您需要确保提供的头文件与当前源文件类型(C或C ++)一致。 Look at the very beginning: '-std=c11', '-x', 'c' '-isystem', Las ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。