Tag: activerecord

如何使用字符串调用名为scope的活动记录

我确定我很想理解使用电话,但我想我可以做这样的事情。 @case_studies = CaseStudy.call(“some_named_scope”) 其中”some_named_scope”也是CaseStudy的命名范围。 我需要使用调用的原因是因为我已经命名了与控制器中的操作名称相同的范围,所以我正在跳这样做。 @case_studies = CaseStudy.call(params[:action]) 编辑 原谅我,我刚刚意识到我正在考虑发送方法,一些单词调用如何卡在我脑海中。 但是@case_studies = CaseStudy.send(params[:action])就像我想的那样工作。

将两个ActiveRecord :: Relation与OR组合,而不是AND,返回一个Relation而不是一个Array,以便稍后能够分页

a和b是ActiveRecord :: Relation对象,它们返回相同类型的对象(在本例中为Micropost对象) a.class => ActiveRecord::Relation b.class => ActiveRecord::Relation a.first => Micropost(…) b.first => Micropost(…) #They both return the same type of objects c=a+b c.class => Array #This is not what i’m looking for c=a|b c.class => Array #Not what i’m looking for either c=(a or b) c.class => ActiveRecord::Relation #But it is just a, […]

在Rails中添加另一个列之前的列

我想把一根柱放在桌子的前面,我知道你可以做到 add_column :customer, :first_name, after: :last_name 但有没有办法:before ?

通过rails中的多个条件查找

我想在Rails中搜索具有多个条件的表。 我正在使用Active record和rails 3.1.0版。 我有Movies对象,并希望在rails中实现以下等效: Select * from Movies where rating = ‘R’ OR rating = ‘PG’ 我尝试了以下但它不起作用 @filtered = Movies.find(:all, :conditions => { :rating => ‘R’, :rating => ‘PG’ }) 你能否提供帮助来编写上面提到的SQL查询的等价物。

如何在ActiveRecord中重用连接?

当我发现这个简单的API没有自动重用连接时,我正在玩Sinatra和ActiveRecord。 #!/usr/bin/env ruby require ‘sinatra’ require ‘active_record’ ActiveRecord::Base.establish_connection( adapter: ‘sqlite3’, database: ‘newsletter.db’ ) ActiveRecord::Schema.define do create_table :subscribers do |t| t.string :email t.timestamps end end class Subscriber < ActiveRecord::Base validates :email, presence: true end class Newsletter < Sinatra::Base set :server, :thin get '/subscribers/:email' do s = Subscriber.find_by_email(params[:email]) if s == nil status 404 else content_type 'application/json' […]

从对象validation消息中删除字段名称

我在一个表单中使用它对一个对象进行了简单的活动记录validation: form.error_messages({:message => ”, :header_message => ”}) 这反过来输出类似“FieldName我的自定义消息”的内容 我需要做的是从错误消息中删除字段名称,但保留我的自定义消息。 任何人都可以指出我正确的方向。

如何使用Rails在一个查询中执行多个语句?

我正在使用带有ActiveRecord和PostgreSQL的Ruby on Rails。 我如何执行多个SQL查询? 我需要它来运行自定义迁移脚本,例如: Foo.connection.execute < ‘20120806120823’; SQL 我不接受用户的数据,所以我不担心sql注入。 可能是MySQL中的CLIENT_MULTI_STATEMENTS东西? 来自MySQL / PHP文档: CLIENT_MULTI_STATEMENTS :告诉服务器客户端可以在一个字符串中发送多个语句(以“;”分隔)。 如果未设置此标志,则禁用多语句执行。 有关此标志的更多信息,请参阅此表后面的注释。

访问父对象属性的“rails方式”是什么?

假设我有模特Doctor和模特Patient 。 Patient belongs_to a Doctor 。 Doctor有一个属性office 。 我想,如果有Patient p ,可以说p.office并访问p ‘s Doctor的office 。 我总是可以写一个方法 class Patient belongs_to :doctor def office self.doctor.office end 但有没有更自动的方式将所有Doctor的属性方法暴露给Patient ? 也许使用method_missing来拥有某种catch-all方法?

ActiveRecord,通过多态属性查找

有这个: class Event true end user = User.create! 我可以: Event.create!(:historizable => user) 但我不能: Event.where(:historizable => user) # Mysql2::Error: Unknown column ‘events.historizable’ in ‘where clause’ 我必须这样做: Event.where(:historizable_id => user.id, :historizable_type => user.class.name) 更新 重现问题的代码: https : //gist.github.com/fguillen/4732177#file-polymorphic_where_test-rb

我可以将模型与其中一个关联转换为YAML格式吗?

我想以YAML格式打印ActiveRecord模型以进行调试。 目前我调用model.to_yaml 。 但它不会返回模型的关联 如何将模型与其中一个关联转换为YAML格式?