Tag: ruby on rails 4

ActionController ::与SSE一起生效无法正常工作

我正在尝试在一个项目中使用Rails 4.0.1中的直播,但我发现问题…… 我有这个动作: def realtime_push response.headers[‘Content-Type’] = ‘text/event-stream’ sse = SSE.new(response.stream) d = Domain.find(params[:domain_id]) begin loop do backlinks = d.backlinks.page(params[:page]).per(10) pagination = render_to_string(:partial => ‘backlinks/pagination’, :layout => false, :locals => { :backlinks => backlinks }) sse.write({ :html => pagination }, :event => ‘pagination’) sleep 1 end rescue IOError # When the client disconnects, we’ll get […]

map(&:id)work但not pluck(:id)

在我的部分代码中,我需要获取用户的id,但是pluck不会抓住它们。 只有map(&:id)可以做到。 我想知道为什么。 我写了一个快速而又脏的代码块来查找发生了什么 def remove_users user_ids p users_to_remove.class users_to_remove = self.users.where(id: user_ids) if users_to_remove.present? self.users -= users_to_remove self.save p users_to_remove.class p users_to_remove Rails::logger.info “\n#{users_to_remove}\n” users_to_remove_map = users_to_remove.map(&:id) p users_to_remove_map Rails::logger.info “\nmap id: #{users_to_remove_map}\n” users_to_remove_pluck = users_to_remove.pluck(:id) p users_to_remove_pluck Rails::logger.info “\npluck id: #{users_to_remove_pluck}\n” #… end self.user_ids end 谁在我的test.log中返回 # map id: [144004] (0.3ms) SELECT “users”.”id” […]

通过深入了解关系来获得数量

我正在使用Rails 4,我希望获得针对特定post的每个post评论的计票数。 架构: Post PostReview PostReviewVote id post_id post_review_id user_id user_id voted_on_date 我有类似的东西: table.table.table-striped.table-hover thead tr th Post Name th Reviews Created For Post th Total Votes in All Reviews For Post tbody – for post in @posts tr td= post.name td= PostReview.where(post_id: post.id).size td= PostReview.where(post_id: post.id).PostReviewVotes.size 但它并没有提取,引用了运行时错误: undefined method `PostReviewVotes’ for # 有什么建议? 我的模型有适当的关联,仅供参考。

Rails4:模型缓存(低级缓存)

我正在尝试缓存Active Record Query的结果。 这是我的用例: User.rb def self.awesome_users_cached Rails.cache.fetch(“awesome_users”) { where(awesome: true) } end UsersController def index all_awesome_users = User.awesome_users_cached less_awesome_users = User.where(less_awesome_user:true).pluck(:id) @users = all_awesome_users.where.not(id: less_awesome_users) end 这是2个问题: self.awesome_users_cached不会缓存结果并最终命中 all_awesome_users最终成为一个数组而不是活动Record,因此我无法从中过滤掉less_awesome_users。 有一个解决方案似乎不再适用于Rails4: Rails缓存与where子句

如何在Postgres的Rails中按天分组和计数?

这个问题我如何按天而不是按日期分组? 展示了如何在Ruby中进行分组。 这可行,但对于大量记录来说会非常慢。 如何做到这一点,以便按日期分组和计算:created_at发生在Postgres内?

使用has_many:through创建连接记录

好吧我以为我很好地遵循了这个答案…… 如何将记录添加到has_many:通过rails中的关联 。 但显然不是。 型号代码: class Transaction < ActiveRecord::Base belongs_to :request has_many :transactories has_many :inventories, through: :transactories end class Inventory < ActiveRecord::Base has_many :transactories has_many :transactions, through: :transactories end class Transactory < ActiveRecord::Base belongs_to :inventory belongs_to :transaction end 我基本上试图将库存与交易相匹配(有东西的人,有想要那些东西的人) 这是我想要实现的流程: 用户在哈希中提供数据,其中key =他们想要的itemlist_id ,值=他们想要的itemlist_id的quantity 。 假设用户想要9中的两个,看起来像这样: { 9 => 2} 对于用户提供的散列中的每个itemlist_id ,我将查看Inventories table并拉出inventory_ids ,其中itemlist_id与用户正在查找的itemlist_id相匹配,而inventory_id的所有者不是他或她自己的用户。 让我们说在Inventories table […]

Rails提供大型文件

我正在开发一个仅向登录用户提供大型video的应用程序。 为了保持这些video的私密性,我将它们放在Rails项目中的私有文件夹中,让Rails为它们提供服务,而不是使用公共文件夹并排除来自apache的请求(以避免直接链接到它们)。 我在控制器中的动作如下所示: def video respond_to do |format| format.mp4{ send_file File.join([Rails.root, “private/videos”, @lesson.link_video1 + “.mp4”]), :disposition => :inline, :stream => true } end end 一切都很完美,但只有小文件,一旦我尝试使用真实文件我收到错误: NoMemoryError (failed to allocate memory) 我在某个地方读到了将send_file用于大文件不是一个好习惯,但是使用其他方法,让apache为文件提供服务,我在向移动苹果设备提供文件时遇到了问题,因为他们没有发送HTTP_REFERER。 你知道这个内存限制有多小吗? 我的video从400MB到2GB(试图减少它们)。 我在这里找到的唯一问题是没有答案来处理rails中assets文件夹中的大型媒体文件

使用Dropzone JS的Carrierwave。 是否可以将这些与嵌套属性一起使用?

目前,我有几种使用嵌套属性的表单。 我最近发现了Dropzone JS,所以我希望以我正在使用的各种forms实现它。 我有两个类似的模型: 游戏 class Games < ActiveRecord::base has_many :screenshots, dependent: :destroy accepts_nested_attributes_for :screenshots, allow_destroy: true end 截图 class Screenshot < ActiveRecord::Base belongs_to :game mount_uploader :ssfile, ScreenshotUploader end 通常,我会用这样的东西来处理它: new.html.haml – content_for :breadcrumbs do %h3.text-center New Game .col-md-12#content .row = render ‘form’ .row .col-md-12 = link_to ‘Back’, games_path _form.html.haml .col-md-12 = form_for @game, :html […]

使用RSpec 3和Rails 4在控制器测试中查找新创建的记录

我在Rails 4中做了一个控制器规范,我想测试由控制器动作创建的记录的属性。 如何找到新创建的记录? 例如,我可以做什么而不是 it ‘Marks a new user as pending’ do post :create, params # I don’t want to use the following line user = User.last expect(user).to be_pending end Rails指南仅简要讨论了控制器测试,其中提到测试Article.count变化为1,而不是如何获得新的ActiveRecord模型。 在Rails 3中找到最新记录的问题是关于Rails 3。 我不愿意使用User.last ,因为默认排序可能是创建日期之外的其他内容。

Heroku LoadError:libruby.so.2.2

当我试图将我的更改推送到Heroku上的分段dyno时,我在构建时得到了这条消息: $ git push staging . . . remote: —–> Installing dependencies using 1.9.7 remote: Running: bundle install –without development:test –path vendor/bundle –binstubs vendor/bundle/bin -j4 –deployment remote: Ignoring bcrypt-3.1.10 because its extensions are not built. Try: gem pristine bcrypt –version 3.1.10 remote: Ignoring json-1.8.3 because its extensions are not built. Try: gem pristine json –version […]