Aut*_*ico 2 activerecord ruby-on-rails-3
在rails 3中,我想做以下事情:
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id")
Run Code Online (Sandbox Code Playgroud)
这有效,但我从DB获得以下内容:
[{"some_other_connection_id":254},{"some_other_connection_id":315}]
Run Code Online (Sandbox Code Playgroud)
现在,那些id-s是我需要的,但我无法做出只给我ID 的查询.我不想对结果进行迭代,只是为了得到那些数字.有什么方法可以让我这样做:
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id").values()
Run Code Online (Sandbox Code Playgroud)
还是那个nautre的东西?
我一直在尝试使用Git-hub中的".select_values()" ,但它只返回"some_other_connection_id".
我不是rails的专家,所以这些信息也可能有用:
"SomeModel"是一个连接表,用于我的其他模型中的多对多关系.所以,我想要做的就是从ID数组中获取连接另一端的所有条目.基本上我有源ID,我想从模型中获取所有目标ID的数据.如果有一种神奇的方式来获取这些,而我不必自己做所有的sql(在活动记录的帮助下),这将是非常好的!
谢谢 :)
小智 9
尝试采摘方法
SomeModel.where(:some => condition).pluck("some_field")
Run Code Online (Sandbox Code Playgroud)
它有点像
SomeModel.where(:some => condition).select("some_field").map(&:some_field)
Run Code Online (Sandbox Code Playgroud)