对.这简直就是拒绝工作.在这几个小时.
专辑模型
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
Run Code Online (Sandbox Code Playgroud)
功能模型
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
Run Code Online (Sandbox Code Playgroud)
join_table1模型
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
Run Code Online (Sandbox Code Playgroud)
join_table1架构
album_id | feature_id
Run Code Online (Sandbox Code Playgroud)
专辑架构
id | title | release_date | genre | artist_id | created_at | updated_at | price | image_path
Run Code Online (Sandbox Code Playgroud)
功能架构
id | feature | created_at | updated_at
Run Code Online (Sandbox Code Playgroud)
在调试测试数据库并运行此集成测试时:
require 'test_helper'
class DataFlowTest < ActionDispatch::IntegrationTest
test "create new user" do
album = albums(:one)
feature = features(:one)
album.features …Run Code Online (Sandbox Code Playgroud) 我有一个Rails模型,它有一个类型为"json"的数据库列:
create_table "games", force: true do |t|
t.json "game_board"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Run Code Online (Sandbox Code Playgroud)
大!现在我该如何使用它?它真的像处理这样的领域一样Hash简单吗?
self.game_board[:player1] = 1
self.game_board[:cards] = cards.to_hash
Run Code Online (Sandbox Code Playgroud)
如果我写这个,那么一切都会按预期工作,所以在未来的客户API调用中我可以这样做吗?:
self.game_board[:player] # And get back the 1 that I put here before
Run Code Online (Sandbox Code Playgroud)
性能如何呢?game_board即使从未读过该字段,每次都会对整个序列进行反序列化吗?每当我改变"Hash"的一部分时,是否会重写该字段(IOW是数据库写入)
我试图使用jQuery计算窗口高度然后将该值应用于DIV(容器div)最后我希望jQuery将元素对齐到页面底部.
<div id="wrapper">
<div id="element-align">Here is the element i wish to align to the bottom</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我有以下哈希:
hash = {'name' => { 'Mike' => { 'age' => 10, 'gender' => 'm' } } }
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式访问年龄:
hash['name']['Mike']['age']
Run Code Online (Sandbox Code Playgroud)
如果我使用Hash#fetch方法怎么办?如何从嵌套哈希中检索密钥?
正如塞尔吉奥所说,做到这一点的方式(不为自己创造一些东西)将是一系列fetch方法:
hash.fetch('name').fetch('Mike').fetch('age')
Run Code Online (Sandbox Code Playgroud) 出于某种原因,Ruby在面向左递归时似乎表现更好.例如:
def left_recursive_factorial(number)
return 1 if number.zero?
left_recursive_factorial(number.pred) * number
end
def right_recursive_factorial(number)
return 1 if number.zero?
number * right_recursive_factorial(number.pred)
end
Run Code Online (Sandbox Code Playgroud)
当我用超过9000()的值调用这些方法时,我会得到不同的结果:
left_recursive_factorial(9001)
# => factorial of 9001
right_recursive_factorial(9001)
# => SystemStackError: stack level too deep
# from (pry):6:in `right_recursive_factorial'
Run Code Online (Sandbox Code Playgroud)
我找不到这种行为的任何解释.
唯一似乎有点相关的事情是LL()解析器有左递归的问题,我想你可以翻转它,但我没有深入挖掘它.
有人可以更详细地解释是什么导致左右递归以不同的方式执行(通常在Ruby中),如果你有可能选择其中一个为什么你会选择它(以及为什么在Ruby中选择) ?
我的一个rails应用程序遇到了一个非常奇怪的问题.我想我可能正在做一些非常愚蠢的事情,而我却无法识别.我的问题是,我似乎错过了大约一半的索引路径.
例如,如果我的控制器是模型foo的"foos",我将拥有:
foos POST /foos(.:format) {:action=>"create", :controller=>"foos"}
Run Code Online (Sandbox Code Playgroud)
但没有GET选项通常如下:
foos GET /foos(.:format) {:action=>"index", :controller=>"foos"}
Run Code Online (Sandbox Code Playgroud)
下面我将向您展示我的实际代码,以帮助我恢复丢失的索引路线.
routes.rb中:
resource :announcements, :controller => "announcements" do
resources :comments
member do
post 'vote'
end
end
Run Code Online (Sandbox Code Playgroud)
公告部分的路线:
announcements POST /announcements(.:format) {:action=>"create", :controller=>"announcements"}
new_announcements GET /announcements/new(.:format) {:action=>"new", :controller=>"announcements"}
edit_announcements GET /announcements/edit(.:format) {:action=>"edit", :controller=>"announcements"}
GET /announcements(.:format) {:action=>"show", :controller=>"announcements"}
PUT /announcements(.:format) {:action=>"update", :controller=>"announcements"}
DELETE /announcements(.:format) {:action=>"destroy", :controller=>"announcements"}
Run Code Online (Sandbox Code Playgroud)
如你所见,没有get/index.在我的控制器中,我有简单的索引方法定义...
def index
@announcements = Announcement.all
respond_to do |format|
format.html
format.xml { render :xml => @announcements }
end
end
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么我没有这个索引路径.它也发生在其他几个控制器上.任何帮助,将不胜感激.
编辑:在控制台中,app.announcements_path除了缺少索引路径的其他错误之外,还返回方法缺失错误.
我期待以下片段:
var = "Not Empty" unless defined? var
var # => nil
Run Code Online (Sandbox Code Playgroud)
回来"Not Empty",但我得到了nil.任何洞察为什么会发生这种情况?
鉴于一堂课,
class MyClass
def index(arg1, arg2="hello")
end
end
Run Code Online (Sandbox Code Playgroud)
是否可以arg2通过某些方法获取默认值Class#instance_method?
我试图用一个int和字符串的混合排序一个数组.举个例子:
a = ["a", "b", 5, "c", 4, "d", "a1", "a12", 3, 13, 2, "13a", "12a"]
Run Code Online (Sandbox Code Playgroud)
我试过了:
a.sort do |x, y|
if x.class == y.class
x <=> y
else
x.class.to_s <=> y.class.to_s
end
end
Run Code Online (Sandbox Code Playgroud)
哪个回报:
[2, 3, 4, 5, 13, "12a", "13a", "a", "a1", "a12", "b", "c", "d"]
Run Code Online (Sandbox Code Playgroud)
我想要的结果是:
[2, 3, 4, 5, "12a", 13, "13a", "a", "a1", "a12", "b", "c", "d"]
Run Code Online (Sandbox Code Playgroud) 我想验证我的第一个字段的形式createViewModal,datepickerCreateModal在dd.mm.yyyy格式.我正在寻找一些正则表达式,我发现它:
/(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d/
Run Code Online (Sandbox Code Playgroud)
但看起来这个regEX并不是很好 - 它从一年开始只拉两位数("20"而不是"2016")
dd.mm.yyyy(11.05.2016)你能给我写一个完整的正则表达式吗?我想我将能够通过bootstrap验证器使用此正则表达式创建回调函数.
如果有人已经有这个正则表达式或类似的解决方案,我会很高兴听到它!
<div class="modal fade" id="createViewModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">New SAR</h4>
</div>
<div class="modal-body">
<div id="formregister">
<form action="" class="form-horizontal" role="form" id="createViewModal">
<p class="qc-errmsg" style="display: none;"> </p>
<div class="form-group">
<label for="Date" class="col-sm-2 control-label">Date</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="datepickerCreateModal" name="Date" placeholder="Date">
</div>
</div>
<div class="form-group">
<label for="Client" …Run Code Online (Sandbox Code Playgroud) ruby ×5
activerecord ×1
arrays ×1
css ×1
internals ×1
jquery ×1
json ×1
natural-sort ×1
parsing ×1
postgresql ×1
rake ×1
recursion ×1
regex ×1
routes ×1
sorting ×1
validation ×1