小编Edw*_*ard的帖子

rails COUNT SELECT DISTINCT

我正在记录用户观看一系列视频的次数.现在,我正在尝试制作每天观看任何视频的用户数量的图表.

UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').count
Run Code Online (Sandbox Code Playgroud)

生成sql

SELECT COUNT(*) AS count_all, DATE(created_at) AS date_created_at FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:43:24' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
Run Code Online (Sandbox Code Playgroud)

这会为每天观看的所有视频产生正确的结果,但正如我所说,我只想向每位用户展示一次.

我想要的sql是

SELECT COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:33:18' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
Run Code Online (Sandbox Code Playgroud)

所以我认为

UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').select('COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created')
Run Code Online (Sandbox Code Playgroud)

会做我想做的事.但这给了

[#<UserVideoWatching >, #<UserVideoWatching …
Run Code Online (Sandbox Code Playgroud)

mysql ruby-on-rails count distinct

15
推荐指数
2
解决办法
2万
查看次数

如何在rails 3.1中添加jquery插件

我想在我的rails应用程序中使用LiveFilter https://github.com/mikemerritt/LiveFilter.它完全符合我的要求 - 使用搜索框过滤现有元素.

我如何将此(或任何其他)jquery插件添加到rails 3.1,以便它可以与资产管道一起使用?如果你知道,它会在3.2中有所不同吗?

jquery ruby-on-rails jquery-plugins ruby-on-rails-3.1

12
推荐指数
1
解决办法
6192
查看次数

Activeadmin阻止我的jQuery工作

我正在使用jquery拖放我的应用程序,它工作正常.

然后我添加了activeadmin,它停止了我的jquery工作.

我收到这个错误

$(".draggable_article_image").draggable is not a function
Run Code Online (Sandbox Code Playgroud)

如果我从active_admin.js中删除此行

//= require active_admin/base
Run Code Online (Sandbox Code Playgroud)

它又开始工作了.

有任何想法吗?

jquery ruby-on-rails ruby-on-rails-3 activeadmin

9
推荐指数
2
解决办法
3954
查看次数

rails - 按天和小时分组

我想创建一个每天每小时创建的项目数组.

我正在跟踪人们的感受,所以我的模型被称为TrackMood它只有一个名为的列mood和时间戳.

如果我做

TrackMood.where(mood: "good").group("hour(created_at)").count
Run Code Online (Sandbox Code Playgroud)

我得到类似{11 => 4,12 => 2,13 => 2,15 => 1}的东西

我这里有2个问题

1如何将日期添加到此中,以便它不仅仅将昨天11点创建的项目添加到今天11点添加的项目中?

2 0如果没有创建任何内容,我如何确保它显示几个小时?

datetime group-by ruby-on-rails

7
推荐指数
1
解决办法
6031
查看次数

Rails has_many通过条件,构建新的

我有用户和组织加入模型UsersOrganisation.用户可能是组织的管理员 - 如果是,则is_admin布尔值为true.

如果我在数据库中手动设置is_admin布尔值,Organisations.admins就像我期望的那样工作.

在控制台中,我可以这样做Organisation.first.users << User.first,它会像我期望的那样创建一个organisations_users条目.

但是,如果我这样做Organisation.first.admins << User.last会创建一个普通用户,而不是管理员,即连接表上的is_admin布尔值未正确设置.

除了直接在连接表中创建条目之外,有没有其他方法可以做到这一点?

class User < ActiveRecord::Base

  has_many :organisations_users
  has_many :organisations, :through => :organisations_users

end

class Organisation <  ActiveRecord::Base

  has_many :organisations_users
  has_many :users, :through => :organisations_users
  has_many :admins, :through => :organisations_users, :class_name => "User", 
            :source => :user, 
            :conditions => {:organisations_users => {:is_admin => true}}

end

class OrganisationsUser < ActiveRecord::Base

  belongs_to :organisation
  belongs_to :user

end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through ruby-on-rails-3

4
推荐指数
1
解决办法
1397
查看次数

Rails 中的自引用 has_many

我已经阅读了很多关于 Rails 中自引用类的内容,但在让它们工作时仍然遇到问题。

我有一类文章,我希望它们能够相互引用,从源文章到结果文章 - 然后能够找到相反的内容。所以我正在尝试使用另一个名为 Links 的类来执行 has_many。

我的架构是

  create_table "articles", :force => true do |t|
    t.string   "name"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "links", :force => true do |t|
    t.integer  "source_id"
    t.integer  "outcome_id"
    t.string   "question"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Run Code Online (Sandbox Code Playgroud)

模型是

class Article < ActiveRecord::Base
    has_many :links_as_source, :foreign_key => "source_id", :class_name => "Link"
    has_many :sources, :through => :links_as_source

    has_many :links_as_outcome, :foreign_key => "outcome_id", :class_name => "Link"
    has_many :outcomes, :through => :links_as_outcome
end
Run Code Online (Sandbox Code Playgroud)

class Link < …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3

3
推荐指数
1
解决办法
427
查看次数

将整数转换为字符串 - rails - mysql

我正在尝试在rails查询中将整数转换为字符串.例如,我想复制这个SQL

SELECT CAST(id AS CHAR) FROM `articles` WHERE `articles`.`name` = 'Frustrated';
Run Code Online (Sandbox Code Playgroud)

这会选择id

Article.where( name: "Frustrated" ).select("id")
Run Code Online (Sandbox Code Playgroud)

但这死了

 Article.where( name: "Frustrated" ).select("CAST (id AS CHAR) ")
Run Code Online (Sandbox Code Playgroud)

即使SQL看起来相同.

有可能做到这一点,我做错了什么?

mysql casting ruby-on-rails

1
推荐指数
1
解决办法
1万
查看次数