我有以下型号: class Section ‘SectionRevision’, :foreign_key => ‘section_id’ has_many :references has_many :revisions, :class_name => ‘SectionRevision’, :foreign_key => ‘section_id’ delegate :position, to: :current_revision def current_revision self.revisions.order(‘created_at DESC’).first end end current_revision是最近创建的revision 。 是否可以将current_revision转换为关联,以便我可以像Section.where(“current_revision.parent_section_id = ‘1’”)那样执行查询? 或者我应该将current_revision列添加到我的数据库,而不是尝试虚拟或通过关联创建它?
假设我有两个关系在同一个模型中保存记录,例如: @companies1 = Company.where(…) @companies2 = Company.where(…) 我怎样才能找到这两种关系的交集,即只有两者之间存在的公司?
我试图在rails中实现multithreading方法,以便我可以非常快速地创建/更新多个记录。 这是我的计划的大纲。 ActiveRecord::Base.transaction do (1..10).each do |i| arr[i] = Thread.new { … … obj.save! } end arr.each {|t| t.join} end 这给了我日志的警告。 DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection. 它给了我一个错误 could not obtain a database connection within […]
我正在尝试配置rails应用程序以远程连接到postgres数据库。 我注意到mysql的连接适配器有选项,指定设置ssl连接所需的信息,但postgres / pg适配器没有等效的选项。 谷歌搜索后,我也找不到任何东西(只通过ssh隧道连接)。 这么简单,试图让rails postgres适配器通过ssl连接死路? 谢谢。 任何帮助或方向表示赞赏。 -H
我有一个Rails应用程序,其中一个模型没有id列。 做一些研究我发现了创建它的迁移: create_table(:the_model, :id => false) do |t| # columns end 现在,在新的迁移中,我想以Rails标准方式添加id列(不使用特定于数据库的sql)。 我怎样才能做到这一点? 我已经尝试过这个没有成功: change_table(:the_model, :id => true) do |t| end
是否可以在创建模型对象期间具有仅可批量分配的属性? 例如, username属性在创建对象时应该是可批量分配的,但在此之后不应该是可分配的(它应该是只读的)。
例如,我有一个博客对象,该博客有很多post。 我想做第一个博客对象的热切加载,并包括说它的前10个post。 目前我会做@blogs = Blog.limit(4) ,然后在视图中使用@blogs.posts.limit(10) 。 我很确定有更好的方法可以通过Blog.include(:posts).limit(:posts=>10) 。 是不是可以限制包含的对象的数量,还是我错过了一些基本的东西?
我有下面的json :response_body => { “status” => “ok”, “um” => { “a” => “a”, “b” => { “c” => “ok” } } 实现我的搜索: LogService.where(response_body: {“status” => “ok”})
我有一个索引页面,我想显示所有用户的个人资料及其相关照片。 我正在使用插件Paperclip来拍照。 在Profiles控制器中,我有实例变量@profile,但它只显示了配置文件表中的表而不是照片表。 @profile = Profile.find(:all, :include => :photos, :joins => “INNER JOIN photos ON photos.profile_id = profiles.id”) 模型如下所示: class Profile < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :profile end 我希望能够在视图中显示的内容如下: John的个人资料(例如姓名,年龄,性别) – John的照片(例如,只显示了一张照片) 玛丽的个人资料 – 玛丽的照片显示在这里 鲍勃的个人资料 – 鲍勃的照片显示在这里
我怎么能在rails active record中做到这一点? 找到所有匹配的模型(created_at +本月之间100天) 编辑:好的,对不起,这不是我在Rails 3.0方式中的活动记录中要做的事情: select distinct p.ID from patients p inner join vaccines_patients vp on p.ID = vp.patient_id inner join vaccines v on v.ID = vp.VACCINE_ID where month(vp.appliedAt + INTERVAL v.duration DAY) = month(now()) 我想得到一个类似的查询,但使用活动记录中的位置。