我是铁杆新手.我看到有很多方法可以找到记录:
find_by_<columnname>(<columnvalue>)find(:first, :conditions => { <columnname> => <columnvalue> }where(<columnname> => <columnvalue>).first看起来所有这些都会产生完全相同的SQL.此外,我相信找到多个记录也是如此:
find_all_by_<columnname>(<columnvalue>)find(:all, :conditions => { <columnname> => <columnvalue> }where(<columnname> => <columnvalue>)是否有经验法则或建议使用哪一个?
这是两个示例代码.
第一个collect:
User.first.gifts.collect(&:id)
Run Code Online (Sandbox Code Playgroud)
第二个pluck:
User.first.gifts.pluck(:id)
Run Code Online (Sandbox Code Playgroud)
他们在表现或其他方面有什么不同吗?
我有一个Release模型medium和country列(以及其他).不应该releases有相同medium/ country组合的共享.
我如何将其写为rails验证?
activerecord ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 rails-activerecord
你如何or在Rails 5 ActiveRecord中进行查询?此外,是否有可能链or与where在ActiveRecord的查询?
ruby activerecord ruby-on-rails rails-activerecord ruby-on-rails-5
当我想要获取今天创建的所有记录时,我应该如何编写条件语句?
使用时,Rails 4引入了弃用警告:uniq => true with has_many:through.例如:
has_many :donors, :through => :donations, :uniq => true
Run Code Online (Sandbox Code Playgroud)
产生以下警告:
DEPRECATION WARNING: The following options in your Goal.has_many :donors declaration are deprecated: :uniq. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
Run Code Online (Sandbox Code Playgroud)
重写上述has_many声明的正确方法是什么?
activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord
我正在尝试覆盖ActiveRecord模型的getter方法.我name在模型中调用了一个属性Category,我希望能够做到这样的事情:
def name
name_trans || name
end
Run Code Online (Sandbox Code Playgroud)
如果name_transattribute不是nil,则返回它,否则返回name属性.我该怎么做?
这应该像这样正常调用:
@category.name
Run Code Online (Sandbox Code Playgroud) 在第一次迁移时,我在列content上声明为字符串Activerecord根据annotate gem将其设置为字符串(255).
在我将应用程序推送到使用postgres的heroku后,如果我在内容中输入一个长度超过255的字符串,我会得到错误
PGError: ERROR: value too long for type character varying(255)
Run Code Online (Sandbox Code Playgroud)
问题是我需要内容包含一个非常长的字符串(自由文本,可能是数千个字符)
谢谢
string postgresql ruby-on-rails ruby-on-rails-3.1 rails-activerecord
如果您偶然发现了这个问题,请检查两个答案,因为我现在正在使用这个问题
我有一个相当大的自定义数据集,我想回到json作为回声.一部分是:
l=Location.find(row.id)
tmp[row.id]=l
Run Code Online (Sandbox Code Playgroud)
但我想做的事情如下:
l=Location.find(row.id).select("name, website, city")
tmp[row.id]=l
Run Code Online (Sandbox Code Playgroud)
但这似乎没有用.我怎么能让这个工作?
谢谢
另外编辑1
,有没有办法可以传递一个只包含我想要包含的属性的数组?
如何回滚失败的rails迁移?我希望这rake db:rollback会撤消失败的迁移,但不会,它会回滚先前的迁移(失败的迁移减去一次).而且rake db:migrate:down VERSION=myfailedmigration也不起作用.我已经碰到了几次,这非常令人沮丧.这是我为复制问题所做的简单测试:
class SimpleTest < ActiveRecord::Migration
def self.up
add_column :assets, :test, :integer
# the following syntax error will cause the migration to fail
add_column :asset, :test2, :integer
end
def self.down
remove_column :assets, :test
remove_column :assets, :test2
end
end
Run Code Online (Sandbox Code Playgroud)
结果:
== SimpleTest: migrating ===================================================== -- add_column(:assets, :test, :integer) -> 0.0932s -- add_column(:asset, :error) rake aborted! An error has occurred, all later migrations canceled: wrong number of arguments (2 for 3)
好吧,让我们回滚:
$ rake db:rollback == …