说 Object embeds_many searched_items
这是文件:
{"_id": { "$oid" : "5320028b6d756e1981460000" },
"searched_items": [
{
"_id": { "$oid" : "5320028b6d756e1981470000" },
"hotel_id": 127,
"room_info": [
{
"price": 10,
"amenity_ids": [
1,
2
]
},
{
"price": 160,
"amenity_ids": null
}
]
},
{
"_id": { "$oid" : "5320028b6d756e1981480000" },
"hotel_id": 161,
"room_info": [
{
"price": 400,
"amenity_ids": [4,5]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想找到具有room_info.amenity_idsIN [2,3] 的"searching_items" .
我试过了
object.searched_items.where('room_info.amenity_ids' => [2, 3])
object.searched_items.where('room_info.amenity_ids' =>{'$in' => [2,3]}
没有运气
是否可以选择带插值的元素?我有一个带字符串的var
inputId = "awesomeInput"
Run Code Online (Sandbox Code Playgroud)
我想选择ID为"awesomeInput"的输入.我试着这样做,就像我通常用jquery做的那样
$("#{inputId}")
Run Code Online (Sandbox Code Playgroud)
console.log告诉我已经选择了某些东西,但我试图在这个对象上执行的任何函数都失败了.没有错误,也没有效果.像这样:
$("#{inputId}").show()
Run Code Online (Sandbox Code Playgroud)
如何选择像这样的jquery元素,而不是显示它?
从我所了解的关于LEFT OUTER JOIN的所有内容中,您想要可以为空的表应位于等号的右侧.如果是这种情况,为什么这两个查询都返回相同的结果:
SELECT *
FROM employees e
LEFT JOIN cars c ON c.employeeID=e.id AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
SELECT *
FROM employees e
LEFT JOIN cars c ON e.id=c.employeeID AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
Run Code Online (Sandbox Code Playgroud)
我有一个模态的以下模板
<div class="ui-modal">
<div class="mask"></div>
<div class="ui-modal-container">
<div class="ui-modal-header">
<h3><%= modal['title'] %></h3>
</div>
<div class="ui-modal-text">
<p><%= modal['description'] %></p>
</div>
<% if ( modal['buttons'] !== 'undefined' ) { %>
<div class="ui-button-container">
<a class="ui-button ui-button-pill <%= modal['buttons'][0]['extra_class'] %> " href="<%= modal['buttons'][0]['href'] %>">
<span class="label">
<span class="section"><%= modal['buttons'][0]['label'] %></span>
</span>
</a>
</div>
<% } %>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我试图填充它的数据:
_data = {
"modal" : {
"title" : "Your address is:",
"description" : "Some desc here",
"buttons" : [
{'extra_class': 'small left', 'href' : '#register/3', …Run Code Online (Sandbox Code Playgroud) 我使用postgres的数组数据类型在rails中有一个标记系统.我正在尝试写一个scope会返回任何包含标签的帖子.到目前为止,我有这个范围工作:
scope :with_tag, ->(tag) { where("tags @> ARRAY[?]", tag) }
Run Code Online (Sandbox Code Playgroud)
我想扩展这个范围,以便我可以同时查询多个标签,理想情况如下:
Post.with_tags(['some', 'tags', 'to', 'query'])
Run Code Online (Sandbox Code Playgroud)
哪个会返回任何Post有这些标签的人.我已经考虑过创建一个类方法来处理输入数组的迭代:
def self.with_tags(args)
# start with empty activerecord relation
# want to output AR relation
results = Post.none
args.each do |tag|
results = results.concat(Post.with_tag(tag))
end
results.flatten
end
Run Code Online (Sandbox Code Playgroud)
但这种方法对我来说很有趣,因为它为每个参数创建了一个新的查询.它也不会返回一个ActiveRecord :: Relation,因为flatten我真的希望将它作为输出.
我可以在OR查询范围内完成我所追求的内容吗?
https://jsfiddle.net/e7mcp6xb/
module.exports = Parentheses = (function() {
var _isParenthesesMatch = function(str) {
var parentheses = str.length;
var rightParentheses = '(';
var leftParentheses = ')';
var rightCount = 0;
var leftCount = 0;
for(i=0;i<=str.length;i++){
if(rightParentheses == str.charAt(i))
{
rightCount++;
}
else if(leftParentheses == str.charAt(i))
{
leftCount++;
}
}
if(rightCount == leftCount){
return true;
}
else(rightCount != leftCount){
return false;
}
}
}());
Run Code Online (Sandbox Code Playgroud) 有什么好方法可以映射和减少长生不老药的列表并将其转换为新列表。
要求: 1. 找到具有相同id 的地图: 2. 合并“role”键的值(即收集所有唯一值)。3. 对于所有其他映射(列表元素),不执行任何操作。
list = [%{"id": 1, "role": ["A", "B"]}, %{"id": 2, "role": ["B", "C"]}, %{"id": 1, "role": ["C", "A"]} ]
Run Code Online (Sandbox Code Playgroud)
需要在以下列表中进行转换:
ans_list = [%{"id": 1, "role": ["A", "B", "C"]}, %{"id": 2, "role": ["B", "C"]}]
Run Code Online (Sandbox Code Playgroud) 给定一个哈希数组,我想创建一个返回哈希的方法,其中键是数组中哈希的唯一值.
例如,我想采取
[
{foo: 'bar', baz: 'bang'},
{foo: 'rab', baz: 'bang'},
{foo: 'bizz', baz: 'buzz'}
]
Run Code Online (Sandbox Code Playgroud)
并返回
{
foo: ['bar', 'rab', 'bizz'],
baz: ['bang', 'buzz']
}
Run Code Online (Sandbox Code Playgroud)
我目前正在使用以下方法完成此任务
def my_fantastic_method(data)
response_data = { foo: [], baz: []}
data.each { |data|
data.attributes.each { |key, value|
response_data[key.to_sym] << value
}
}
response_data.each { |key, value| response_data[key] = response_data[key].uniq }
response_data
end
Run Code Online (Sandbox Code Playgroud)
有更优雅的方式吗?谢谢!
我正在使用ruby on rails创建一个博客应用程序.我能够允许用户添加帖子,但在页面顶部,首先创建的帖子首先出现.我希望最新的博客文章首先出现.我该怎么做呢?让我知道我需要在这里添加哪些代码文件.
我的文章控制器
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def index
@articles = Article.paginate(:page => params[:page], :per_page => 10)
end
def show
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
private
def article_params
params.require(:article).permit(:title, :text, :datee)
end
Run Code Online (Sandbox Code Playgroud) ruby ×3
jquery ×2
postgresql ×2
arrays ×1
class ×1
coffeescript ×1
elixir ×1
html ×1
javascript ×1
mongodb ×1
mongoid ×1
sql ×1
templates ×1