我一直在玩STI和belongs_to/has_many关系,我有点困惑.
基于类似于以下的模型配置,我有几个问题:
class Parental < ActiveRecord::Base
end
class Mother < Parental
has_many :babies
end
class Father < Parental
has_many :babies
end
class Baby < ActiveRecord::Base
belongs_to :??????
end
Run Code Online (Sandbox Code Playgroud)
Baby属于什么?babies桌面上为外键命名/添加什么?我首先想到的是添加parental_id到babies具有类似的方法沿着Baby#owner该执行以下操作:
谢谢!
ruby-on-rails has-many single-table-inheritance sti ruby-on-rails-3
我想知道如何使用open-uri打开多个并发连接?我认为我需要使用线程或纤维,但我不确定.
示例代码:
def get_doc(url)
begin
Nokogiri::HTML(open(url).read)
rescue Exception => ex
puts "Failed at #{Time.now}"
puts "Error: #{ex}"
end
end
array_of_urls_to_process = [......]
# How can I iterate over items in the array in parallel (instead of one at a time?)
array_of_urls_to_process.each do |url|
x = get_doc(url)
do_something(x)
end
Run Code Online (Sandbox Code Playgroud) 我看过很多人推荐zsh over bash进行ruby开发,而我却无法理解zsh提供什么比bash更好?
这篇文章的回答是:
使用zsh而不是bash时,有什么好处,特别是ruby开发人员?
谢谢!
我经常听到这句话,并不完全理解它的含义.这是什么意思?如果可能的话,有一个例子吗?
谢谢!
我正在关注Ryan Bates 关于在Rails中使用内置的PostgresQL全文搜索的优秀教程.我目前正在使用pg_search gem un-indexed没问题,但我需要提高性能.我正在使用指定了"英语"字典的tsvector.
我正在使用PostgreSQL版本9.1.4
根据Ryan的说明,我使用此代码运行了一个新的迁移,指定了我想要创建的两个新索引.这是架构首先:
create_table "references", :force => true do |t|
t.string "title"
t.string "type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "public_url"
t.string "content_type"
t.integer "file_size"
t.text "overview"
t.text "body"
t.text "full_text"
t.integer "folder_id"
end
Run Code Online (Sandbox Code Playgroud)
我的迁移看起来像这样:
def up
execute "create index references_title on references using gin(to_tsvector('english', title))"
execute "create index references_full_text on references using gin(to_tsvector('english', full_text))"
end
def down
execute "drop index references_title"
execute "drop index references_full_text"
end
Run Code Online (Sandbox Code Playgroud)
我也继续在application.rb中取消注释:sql选项
config.active_record.schema_format …Run Code Online (Sandbox Code Playgroud) 是否可以使用动态查找/创建方法跳过验证?
例如,通过常规保存,我可以执行以下操作:
p = Post.new
p.title = nil
p.body = nil
p.save(:validate => false)
Run Code Online (Sandbox Code Playgroud)
愿意做同样的事情find_or_create_by_title.
class Track < ActiveRecord::Base
has_many :artist_tracks
has_many :owning_artists,
-> { where(:artist_tracks => { :artistic_role_id => 1 }) },
:through => :artist_tracks,
:source => :artist
end
class ArtistTrack < ActiveRecord::Base
belongs_to :artist
belongs_to :track
belongs_to :artistic_role
end
class Artist < ActiveRecord::Base
has_many :artist_tracks
has_many :tracks, :through => :artist_tracks
end
Run Code Online (Sandbox Code Playgroud)
# artist_tracks.artistic_role_id is properly set to "1"
2.0.0p195 :003 > Track.last.owning_artists
Track Load (1.1ms) SELECT "tracks".* FROM "tracks" ORDER BY "tracks"."id" DESC LIMIT 1
Artist Load (0.8ms) SELECT "artists".* …Run Code Online (Sandbox Code Playgroud) ruby activerecord ruby-on-rails associations ruby-on-rails-4
我有几个类只用于rake任务.我意识到rake任务通常是@ lib/tasks/whatever.rake,但我应该在哪里放置支持类?
谢谢!
我有一段代码我试图用nokogiri解析,看起来像这样:
<td class="j">
<a title="title text1" href="http://link1.com">Link 1</a> (info1), Blah 1,<br>
<a title="title text2" href="http://link2.com">Link 2</a> (info1), Blah 1,<br>
<a title="title text2" href="http://link3.com">Link 3</a> (info2), Blah 1 Foo 2,<br>
</td>
Run Code Online (Sandbox Code Playgroud)
我可以使用以下内容访问td.j的源代码:
data_items = doc.css("td.j")
我的目标是将每一行分成一组哈希.我能看到的唯一逻辑分裂点是拆分BR,然后在字符串上使用一些正则表达式.
我想知道是否有一个更好的方法来做这个可能只使用nokogiri?即使我可以使用nokogiri来吸取3行项目,它也会让我更容易,因为我可以在.content结果上进行一些正则表达式解析.
不知道如何使用Nokogiri抓住以br结尾的行 - 我应该使用xpath吗?任何方向表示赞赏!谢谢
在Rails应用程序中创建管理区域的典型格式/结构是什么?
具体来说,我被困在这些主题的附近:
谢谢!
ruby ×7
activerecord ×2
admin ×1
associations ×1
bash ×1
fibers ×1
has-many ×1
idioms ×1
nokogiri ×1
open-uri ×1
parsing ×1
pg-search ×1
phrase ×1
postgresql ×1
rake ×1
shell ×1
sti ×1
validation ×1
xpath ×1
zsh ×1