我的gem文件包含:
gem "breadcrumb", :git => "git@github.com:mjacobus/Breadcrumb.git"
gem "simple_menu", :git => "git@github.com:mjacobus/simple_menu.git"
Run Code Online (Sandbox Code Playgroud)
第一个gem按预期安装,但第二个失败.
Updating git@github.com:mjacobus/simple_menu.git
Fetching gem metadata from https://rubygems.org/........
Could not find gem 'simple_menu (>= 0) ruby' in git@github.com:mjacobus/simple_menu.git (at master).
Source does not contain any versions of 'simple_menu (>= 0) ruby'
Run Code Online (Sandbox Code Playgroud)
类似的帖子指出缺少.gemspec文件,但此文件存在.https://github.com/mjacobus/simple_menu/blob/master/simple_menu.gemspec
我可能正在做一些别人的眼睛能看到的蠢事.
我将repo克隆到vendor/plugins文件夹并更改了我的Gemfile
gem "simple_menu", :path => "vendor/plugins/simple_menu"
Run Code Online (Sandbox Code Playgroud)
捆绑安装问题现在出现错误:
bundle install
Fetching gem metadata from https://rubygems.org/.......
Could not find gem 'simple_menu (>= 0) ruby' in source at vendor/plugins/simple_menu.
Source does not contain any versions of 'simple_menu …Run Code Online (Sandbox Code Playgroud) 我有四张桌子:
与字段的争论
评论与
用户
昵称与
每个论点都有很多评论,
每条评论都属于一个用户,
每个用户在参数中都有一个特定的昵称.
当我从DB获取参数注释时,我想包括每个作者的昵称.
答案是关于我不知道怎么写的ActiveRecord查询.
我试过了
@argument.comments.includes(:user => :nicknames)
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用,当我通过nickname = @ argument.nicknames.find_by_user_id(comment.user.id)获得昵称时,它执行查询...
[1m[36mNickname Load (0.6ms)[0m [1mSELECT "nicknames".* FROM "nicknames" WHERE "nicknames"."argument_id" = 59 AND "nicknames"."user_id" = 9 LIMIT 1[0m
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我有以下结构
a = [['a', 'b', 'c'], ['d', 'e', 'f'], [['g', 'h', 'i'], ['l', 'm', 'n']]]
Run Code Online (Sandbox Code Playgroud)
我想获得以下内容:
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['l', 'm', 'n']]
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
a.flatten => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n']
a.flatten(1) => ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i'], ['l', 'm', 'n']]
Run Code Online (Sandbox Code Playgroud)
我现在找到的解决方案是将初始结构更改为以下格式:
b = [[['a', 'b', 'c']], [['d', 'e', 'f']], [['g', 'h', 'i'], ['l', 'm', 'n']]]
Run Code Online (Sandbox Code Playgroud)
然后打电话
b.flatten(1) => [['a', 'b', 'c'], ['d', 'e', …Run Code Online (Sandbox Code Playgroud) 当仅Rails API中发生错误时,服务器会以以下格式响应json中的错误:{"status":500,"error":"Internal Server Error"}。
其他错误的格式也相同:{"status":404,"error":"Not Found"}。
我想用以下格式呈现错误:{"errors": [{status: 404, title: "Not found"}]}。
基本上,我想将所有错误的格式都更改为相同的格式,但是我没有找到解决方法。
我知道我可以使用(例如)rescue_from ActiveRecord::RecordNotFound, with: :my_method并覆盖单个异常,但这意味着我必须列出所有可能的异常并在Rails已经这样做的情况下返回适当的代码。
我正在寻找一种可以被覆盖的方法,并且可以用来更改Rails的“默认”错误格式。
我有以下代码:
def find_animal
animal_type = AnimalKind.find_by(conditions) ||
Animal.find_by(conditions) ||
Breed.find_by(conditions)
end
Run Code Online (Sandbox Code Playgroud)
我想将它重构为:
def find_animal
animals = [AnimalKind, Animal, Breed]
animal_type = animals.first_not_nil { |m| m.find_by(conditions) }
end
Run Code Online (Sandbox Code Playgroud)
关于如何实现first_not_nil方法的任何想法?