小编Tob*_*ner的帖子

如何用ActiveRecord/Rails表达NOT IN查询?

只是为了更新这个,因为似乎有很多人来到这里,如果你使用Rails 4,请查看TrungLê`和VinniVidiVicci的答案.

Topic.where.not(forum_id:@forums.map(&:id))

Topic.where(published:true).where.not(forum_id:@forums.map(&:id))
Run Code Online (Sandbox Code Playgroud)

我希望有一个简单的解决方案不涉及find_by_sql,如果不是,那么我想这将必须工作.

我发现这篇文章引用了这个:

Topic.find(:all, :conditions => { :forum_id => @forums.map(&:id) })
Run Code Online (Sandbox Code Playgroud)

这是一样的

SELECT * FROM topics WHERE forum_id IN (<@forum ids>)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法NOT IN解决这个问题,例如:

SELECT * FROM topics WHERE forum_id NOT IN (<@forum ids>)
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-activerecord

198
推荐指数
6
解决办法
13万
查看次数

在rails中将DateTime字符串转换为UTC

我有一个像这样的字符串:

"2010-01-01 12:30:00"
Run Code Online (Sandbox Code Playgroud)

我需要从当前本地时区转换为UTC.

我试过这个,但似乎认为字符串已经是UTC了.

"2010-01-01 12:30:00".to_datetime.in_time_zone("Central Time (US & Canada)")
=> Fri, 01 Jan 2010 06:30:00 CST -06:00
Run Code Online (Sandbox Code Playgroud)

我不知道从哪里开始.

从我的评论中添加了这个:

>> Time.zone = "Pacific Time (US & Canada)"
=> "Pacific Time (US & Canada)"
>> Time.parse("2010-10-27 00:00:00").getutc
=> Wed Oct 27 06:00:00 UTC 2010
>> Time.zone = "Mountain Time (US & Canada)"
=> "Mountain Time (US & Canada)"
>> Time.parse("2010-10-27 00:00:00").getutc
=> Wed Oct 27 06:00:00 UTC 2010
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

ruby ruby-on-rails

36
推荐指数
3
解决办法
4万
查看次数

从每个数组项rails中删除空格

我找到了这段代码:

.each_key {|a| self[a].strip! if self[a].respond_to? :strip! }
Run Code Online (Sandbox Code Playgroud)

在这个网站上:http://granth.ca/2006/02/strip-whitespace

但我认为这是一个哈希,我试图对数组做同样的事情.

我尝试了一些不同的东西,但无法弄清楚.

感谢您的任何帮助

ruby arrays ruby-on-rails

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

scss文件rails中的asset_path

我在生产中遇到asset_path问题.Rails 3.1.1

#config/environments/development.rb
Scc::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3.1 asset-pipeline

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

在collection_select上使用大写

如果在我找不到之前已经回答了这个问题.

我有以下内容:

= f.collection_select :sex_id, @sexes, :id, :name
Run Code Online (Sandbox Code Playgroud)

这在控制器中:

@sexes = Sex.all
Run Code Online (Sandbox Code Playgroud)

性别都以小写形式存储,如下所示:

id|name
 1|steer
 2|heifer
 3|holstein
Run Code Online (Sandbox Code Playgroud)

我需要他们用Capital First字母输出:

Steer
Heifer
Holstein
Run Code Online (Sandbox Code Playgroud)

我试过了:

= f.collection_select :sex_id, @sexes, :id, :name.capitalize
= f.collection_select :sex_id, @sexes, 'id', 'name'.capitalize
Run Code Online (Sandbox Code Playgroud)

但它们不起作用,我并没有真正期待它们,但在发布之前必须先尝试一下.

collections ruby-on-rails capitalize

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

has_many通过多个数据库连接

如何通过多个数据库连接来创建has_many?

我有一个名为"master"的数据库,用于保存位置信息.这是从单独的应用程序更新.用户可以访问许多位置,但所有其他模型都位于另一个名为"预算"的数据库中.以下是模型的设置方法.

# place.rb
class Place < ActiveRecord::Base
  belongs_to :user
  belongs_to :location
end

# user.rb
class User < ActiveRecord::Base
  has_many :locations, :through => :places
  has_many :places
end

# location.rb
class Location < ActiveRecord::Base
  establish_connection "master"
  has_many :places
  has_many :users, :through => :places
end
Run Code Online (Sandbox Code Playgroud)

当我通过irb运行命令时,我得到以下内容

> Location.first.places.create(:user_id => 1)
> #<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43",  updated_at: "2011-11-28 20:58:43">

> Location.first.places
> [#<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43", updated_at: "2011-11-28 20:58:43">]

> Location.first.users …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails has-many-through ruby-on-rails-3.1

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

匹配时的情况

有没有办法让这个工作正确的情况下?

field = "head_count_2011_10_75"
case field
  when match(/head_count_\d{4}_\d{1,2}_\d{1,4}/i)
    puts "regex 1"
  when match(/dmi_\d{4}_\d{1,2}_\d{1,4}/i)
    puts "regex 2
end
Run Code Online (Sandbox Code Playgroud)

我知道如果符合以下条件我可以做到:

if field.match(/head_count_\d{4}_\d{1,2}_\d{1,4}/i)
  puts "regex 1"
elsif field.match(/dmi_\d{4}_\d{1,2}_\d{1,4}/i)
  puts "regex 2"
end
Run Code Online (Sandbox Code Playgroud)

只是寻找更清洁的解决方案.

regex ruby-on-rails switch-statement

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

haml new tr每3件事

我有以下代码(简化以达到目的):

    - @assumptions[1].each do |ca|
      - if count % 3 == 1
        %tr
          %th
            = ca.value
      - else
        %th
          = ca.value
      - count = count + 1
Run Code Online (Sandbox Code Playgroud)

我不知道如何在haml中使这个工作让每4个项目创建一个新标签.

这就是它的输出方式:

  <tr>
    <tr> 
      <th> 
        700.0
      </th> 
    </tr> 
    <th> 
      1235.0
    </th> 
    <th> 
      0.8
    </th> 
    <th> 
      650.0
    </th> 
  <tr> 
    <th> 
      1050.0
    </th> 
  </tr> 
  <th> 
    0.2
  </th> 
</tr>
Run Code Online (Sandbox Code Playgroud)

这是我希望它输出的方式:

<tr>
  <th>
    700.0
  </th>
  <th>
    1235.0
  </th>
  <th>
    0.8
  </th>
</tr>
<tr>
  <th>
    650.0
  </th>
  <th>
    1050.0
  </th>
</tr>
Run Code Online (Sandbox Code Playgroud)

我希望这是有道理的.

haml ruby-on-rails

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

capistrano多个用户的一个用户名

我有多个开发人员在rails中处理同一个应用程序.我试图让capistrano设置使用一次登录(最好使用公钥和密码).我似乎无法在网上找到任何涉及此内容的内容.谁能指出我正确的方向?

谢谢

capistrano ruby-on-rails

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

多行PHP上的数字的正则表达式

我有一个看起来像这样的文件(是的,换行符是正确的):

39                                              9
30 30 30 31 34 30 30 32 33 32 36 30 31 38 0D 0A 00014002326018..
39 30 30 30 31 34 30 30 32 33 32 36 30 35 34 0D 900014002326054.
0A                                              .
39 30 30 30 31 34 30 30 32 33 32 36 30 39 31 0D 900014002326091.
0A                                              .
39 30 30 30 31 34 30 30 32 33 32 36 31 36 33 0D 900014002326163.
0A                                              .
39                                              9
30 …
Run Code Online (Sandbox Code Playgroud)

php regex

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