Tag: rails activerecord

Rails:使用where子句查找深层嵌套的关联

我有两个模型加入了has_many:通过关系: class Publication :publication_contributors end class Contributor :publication_contributors end class PublicationContributor < ActiveRecord::Base belongs_to :publication belongs_to :contributor end (关于我的PublicationContributor模型的一些不寻常和重要的是它不仅仅有一对数据库ID,它还有一个名为contributor_type的字符串属性。这个字符串可能包含诸如“Author”或“Translator”或“Publisher”之类的角色。我不相信这是问题,但解决方案仍然必须考虑到它。) 我想找一个具有特定贡献者的出版物,如下所示: Publication .joins(:publication_contributors => :contributor) .where(:publication_contributors => {:contributor_type => “Author”, :contributor => {:name => params[:authors]}}) 一切正常,直到我进入嵌套:贡献者,此时SQL溅出: Mysql2::Error: Unknown column ‘publication_contributors.contributor’ in ‘where clause’ 它不是在寻找publication_contributors.contributor_id,而是在寻找不存在的publication_contributors.contributor。 我在代码中做错了吗? 我找不到像这样具有深层嵌套关联的where子句的任何其他示例。 也许它甚至不可能? 更新: 生成的SQL ←[1m←[35mPublication Load (0.0ms)←[0m SELECT `publications`.* FROM `publicati […]

在模型之间共享枚举声明值

我在以下属性上应用枚举 : transparency 相同的属性(使用枚举)用于两个不同的模型: Category和Post 是否可以在模型之间共享枚举值,以避免代码重复: enum transparency: %w(anonymous private public)

路由关注和多态模型:如何共享控制器和视图?

鉴于路线: Example::Application.routes.draw do concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :forums do resources :forum_topics, concerns: :commentable end end 而型号: class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end 当我编辑或添加评论时,我需要回到“可评论”对象。 不过,我有以下几个问题: 1)根据父对象, comments_controller.rb的redirect_to会有所不同 2)对视图的引用也会有所不同 = simple_form_for comment do |form| 是否有实用的方法来共享此comment资源的视图和控制器?

迭代数据库中的每条记录 – Ruby on Rails / ActiveRecord

n00b问题。 我正在尝试遍历数据库中的每个用户记录。 伪代码可能看起来像这样: def send_notifications render :nothing => true # Randomly select Message record from DB @message = Message.offset(rand(Message.count)).first random_message = @message.content @user = User.all.entries.each do @user = User.find(:id) number_to_text = “” @user.number = number_to_text #number is a User’s phone number puts @user.number end end 有人可以填写我这样做的最佳方法吗? 语法的一点帮助也会很棒:)

什么是Ruby on Rails ORM的外行人的术语? 请解释

我在Ruby on Rails中无法理解ORM。 据我所知,表/列和对象/属性之间存在1:1的关系。 所以每条记录都是一个对象。 究竟什么是模型? 我知道它映射到一张桌子。 我真正追求的是对上述内容的更深入理解。 预先感谢您的帮助 我是一名从PHP到Ruby on Rails的Web开发人员。

Rails counter_cache没有正确更新

使用Rails 3.1.3,我试图弄清楚为什么我们的计数器缓存在通过update_attributes更改父记录ID时没有正确更新。 class ExhibitorRegistration true end class Event :destroy end describe ExhibitorRegistration do it ‘correctly maintains the counter cache on events’ do event = Factory(:event) other_event = Factory(:event) registration = Factory(:exhibitor_registration, :event => event) event.reload event.exhibitor_registrations_count.should == 1 registration.update_attributes(:event_id => other_event.id) event.reload event.exhibitor_registrations_count.should == 0 other_event.reload other_event.exhibitor_registrations_count.should == 1 end end 此规范失败,表明事件上的计数器缓存未递减。 1) ExhibitorRegistration correctly […]

Rails 3.1:无法在添加它的同一个迁移中写入列

我有一个可以正常运行的add_column迁移。 但是,在运行它并启动控制台后,我会发现first_name和last_name列完全为空。 我试过用save! 相反,它具有相同的效果 – 没有报告错误。 这是原始的: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name = 'first name' u.last_name = 'last name' u.save end end end 我还认为这可能是一些类加载问题,所以我插入了User行来强制用户类在循环之前重新加载。 没有骰子。 当我将其拆分为两次迁移时,实现了期望的效果。 有人对此有解释吗? 我发誓我甚至在过去迁移的同一个项目中做过这个。 其他说明:设计用户引擎,在运行迁移之前将新列添加到User类中的attr_accessible 。

在ActiveRecord中存储枚举值并转换为字符串以进行显示的最佳方法

我试图弄清楚在activerecord中存储枚举值的最佳方法是什么,但将其转换为“标题”以便在应用中显示。 IE 评论枚举: UNREVIEWED = {:title => “Unreviewed”, :name => “UNREVIEWED”} REVIEWED = {:title => “Reviewed”, :name => “REVIEWED”} FLAGGED = {:title => “Flagged as inappropriate”, :name => “FLAGGED”} 所以在java版本中我习惯在数据库中存储ENUMs名称即(REVIEWED),然后将该名称转换为服务器上的实际枚举,以便我可以在其上调用辅助方法,即: review = Review.valueOf(review) review.title() 有没有类似的东西我可以在rails中做到这一点? 仅供参考我们试图保持我们的应用程序超小,所以如果我能够很容易地实现这个或类似的东西没有一个伟大的创业板。 任何“标准”的方式,我想我不是第一个与这个问题斗争的人? 谢谢!

Rails嵌套连接条件的Activerecord

我正在尝试用条件编写嵌套连接查询。 我现在的查询是: Event.joins(:store => :retailer).where(store: {retailer: {id: 2}}) 其中输出以下SQL: SELECT “events”.* FROM “events” INNER JOIN “stores” ON “stores”.”id” = “events”.”store_id” INNER JOIN “retailers” ON “retailers”.”id” = “stores”.”retailer_id” WHERE “store”.”retailer_id” = ‘— :id: 2 ‘ 还有以下错误: SQLite3::SQLException: no such column: store.retailer_id: SELECT “events”.* FROM “events” INNER JOIN “stores” ON “stores”.”id” = “events”.”store_id” INNER JOIN “retailers” ON […]

Rails Paperclip仅适用于图像吗?

是否有用于管理与ActiveRecord连接的文件附件的rails库? 我知道回形针 ,但它似乎主要适合图像。 他们确实在github项目页面上提到了音频和pdf文件,但是对于不同文件类型的使用没有进一步的解释。 如果您上传了音频文件,则:style会改变其含义。 因此,不同的文件大小不会以二维分辨率表示,而是以比特率表示。 回形针有什么替代品吗? 或者有可能不仅仅将imagemagick与paperclip链接,例如ffmpeg ?