当你Something.find(array_of_ids)在Rails中,所得到的数组的顺序不依赖的顺序array_of_ids.
有没有办法找到并保存订单?
ATM我根据ID的顺序手动对记录进行排序,但这有点蹩脚.
UPD:如果可以使用:orderparam和某种SQL子句指定顺序,那么如何?
#new_record?function确定是否已保存记录.但它总是假的after_save.有没有办法确定记录是新创建的记录还是更新的旧记录?
我希望不要使用另一个回调,比如before_create在模型中设置一个标志或者需要另一个查询到db.
任何建议表示赞赏.
编辑:需要在after_save挂钩中确定它,对于我的特定用例,没有updated_at或updated_on时间戳
Rails引入了验证模型内属性的新方法.我用的时候
validates :title, :presence => true
Run Code Online (Sandbox Code Playgroud)
它工作,但当我尝试添加自定义消息
validates :title, :presence => true,:message => "Story title is required"
Run Code Online (Sandbox Code Playgroud)
生成错误
Unknown validator: 'message'
Run Code Online (Sandbox Code Playgroud) 我有一个对象数组,我们称之为Indicator.我想def self.subjects在这个数组上运行Indicator类方法(变种类,范围等).我知道在一组对象上运行类方法的唯一方法是让它们成为ActiveRecord :: Relation.所以我最终要求添加一个to_indicators方法Array.
def to_indicators
# TODO: Make this less terrible.
Indicator.where id: self.pluck(:id)
end
Run Code Online (Sandbox Code Playgroud)
有时我会链接相当多的这些范围来过滤类方法中的结果.所以,即使我在ActiveRecord :: Relation上调用一个方法,我也不知道如何访问该对象.我只能通过它来获取它的内容all.但是all是一个数组.那么我必须将该数组转换为ActiveRecord :: Relation.例如,这是其中一种方法的一部分:
all.to_indicators.applicable_for_bank(id).each do |indicator|
total += indicator.residual_risk_for(id)
indicator_count += 1 if indicator.completed_by?(id)
end
Run Code Online (Sandbox Code Playgroud)
我想这可以归结为两个问题.
where每次都做.def self.subjectsActiveRecord :: Relation上运行类型方法时,如何访问ActiveRecord :: Relation对象本身?谢谢.如果我需要澄清任何事情,请告诉我.
使用Postgres和Activerecord在多列中查找具有重复值的记录的最佳方法是什么?
我发现这个解决方案在这里:
User.find(:all, :group => [:first, :email], :having => "count(*) > 1" )
但它似乎不适用于postgres.我收到这个错误:
PG :: GroupingError:错误:列"parts.id"必须出现在GROUP BY子句中或用于聚合函数
我想执行一个更新原始sql,如下所示:
update table set f1=? where f2=? and f3=?
Run Code Online (Sandbox Code Playgroud)
这个SQL将由执行ActiveRecord::Base.connection.execute,但我不知道如何将动态参数值传递给方法.
有人可以给我任何帮助吗?
class Agents << ActiveRecord::Base
belongs_to :customer
belongs_to :house
end
class Customer << ActiveRecord::Base
has_many :agents
has_many :houses, through: :agents
end
class House << ActiveRecord::Base
has_many :agents
has_many :customers, through: :agents
end
Run Code Online (Sandbox Code Playgroud)
如何添加Agents模型Customer?
这是最好的方法吗?
Customer.find(1).agents.create(customer_id: 1, house_id: 1)
Run Code Online (Sandbox Code Playgroud)
以上工作正常从控制台,但我不知道如何在实际应用程序中实现这一点.
想象一下,为客户填写的表格也house_id作为输入.然后在我的控制器中执行以下操作?
def create
@customer = Customer.new(params[:customer])
@customer.agents.create(customer_id: @customer.id, house_id: params[:house_id])
@customer.save
end
Run Code Online (Sandbox Code Playgroud)
总的来说,我对如何在has_many :through表中添加记录感到困惑?
所以我对db执行查询,我有一个完整的对象数组:
@attachments = Job.find(1).attachments
Run Code Online (Sandbox Code Playgroud)
现在,我有对象的数组我不想执行另一个数据库查询,但我想基础上的滤镜阵列Attachment对象的file_type,这样我可以有一个列表attachments,其中文件类型为'logo',然后另一个列表attachments哪里文件类型是'image'
像这样的东西:
@logos = @attachments.where("file_type = ?", 'logo')
@images = @attachments.where("file_type = ?", 'image')
Run Code Online (Sandbox Code Playgroud)
但是在内存中而不是db查询.
我正在尝试做一些我认为很简单的事情,但似乎并不是这样.
我有一个有很多职位空缺的项目模型.
class Project < ActiveRecord::Base
has_many :vacancies, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
我想得到所有至少有1个空缺的项目.我试过这样的事情:
Project.joins(:vacancies).where('count(vacancies) > 0')
Run Code Online (Sandbox Code Playgroud)
但它说
SQLite3::SQLException: no such column: vacancies: SELECT "projects".* FROM "projects" INNER JOIN "vacancies" ON "vacancies"."project_id" = "projects"."id" WHERE ("projects"."deleted_at" IS NULL) AND (count(vacancies) > 0).
这似乎相当简单,但我不能让它出现在Google上.
如果我有:
class City < ActiveRecord::Base
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :city
end
Run Code Online (Sandbox Code Playgroud)
我想找到所有没有照片的城市.我很乐意打电话给...
City.where( photos.empty? )
Run Code Online (Sandbox Code Playgroud)
......但那不存在.那么,你如何做这种查询?
更新: 现在找到原始问题的答案,我很好奇,你如何构建逆?
IE:如果我想创建这些作为范围:
scope :without_photos, includes(:photos).where( :photos => {:city_id=>nil} )
scope :with_photos, ???
Run Code Online (Sandbox Code Playgroud) activerecord ×10
ruby-on-rails ×10
ruby ×3
sql ×2
activemodel ×1
has-many ×1
postgresql ×1
rawsql ×1
validation ×1