Tag: activerecord

使用find_by_id在RSpec中获取不存在的记录时引发RecordNotFound

我在products_controller_spec.rb中编写了这个规范,用于在不存在的记录上调用destroy时测试重定向: it “deleting a non-existent product should redirect to the user’s profile page with a flash error” do delete :destroy, {:id => 9999} response.should redirect_to “/profile” flash[:error].should == I18n.t(:slideshow_was_not_deleted) end 这是products_controller.rb中的控制器操作: def destroy product = Product.find_by_id(params[:id]) redirect_to “profile” if !product if product.destroy flash[:notice] = t(:slideshow_was_deleted) if current_user.admin? and product.user != current_user redirect_to :products, :notice => […]

如何使用rpec在Rails测试环境中加载seed.rb?

我有以下seeds.rb文件: State.create [ {:name => “Alabama”, :abbreviation => “AL” }, {:name => “Alaska”, :abbreviation => “AK” }, {:name => “Arizona”, :abbreviation => “AZ” }, {:name => “Arkansas”, :abbreviation => “AR” }, {:name => “California”, :abbreviation => “CA” }, {:name => “Colorado”, :abbreviation => “CO” }, {:name => “Connecticut”, :abbreviation => “CT” }, {:name => “Delaware”, […]

ActiveRecord:定义关联时,将类而不是字符串传递给class_name

在定义关联时,是否存在传递类而不是字符串的含义或陷阱? belongs_to :owner, class_name: User 相反: belongs_to :owner, class_name: “User”

使用装饰器,(rails)无法推断ActiveRecord :: Base的装饰器

我在使用装饰器时遇到了麻烦。 我之前从未使用过一个,而且我一直试图使用一个关于我为打破一些电子邮件而做的事情。 然而,因为我之前从未使用过,所以我甚至无法用装饰器做一些非常简单的事情,而且我认为它存在某种forms的设置问题。 我知道除了我的小function之外的所有内容(也就是gemfile等)都是最新的和正确的。 我得到的错误很简单, 无法推断ActiveRecord :: Base的装饰器。 现在我有一个几乎为空的控制器,但在其中,我保存了活动记录部分。 class Admin::ReceivedEmailsController < Admin::ApplicationController 以我的观点为标题, _receive_email.html.haml 我现在所做的一切都是如此: %td= received_email.decorate 我的装饰师 class Admin::ReceivedEmailsDecorator < Admin::ApplicationDecorator def received_email if can? :update, @customer received_email.content else "You need to have the correct admin access to view the email" end end 我觉得这对我来说应该是一件非常重要的事情,但是我不确定它是什么。 有人会知道我错过了什么吗?

找不到关联,Rails 3

class Membership < ActiveRecord::Base belongs_to :role belongs_to :user end class User :memberships end class Role :memberships end 和我的观点 我的视图上有下一个错误 – 无法找到关联:模型中的成员资格用户和我无法理解为什么会发生这种情况。

Rails 2.3.x – 这个ActiveRecord范围如何工作?

我正在处理的项目中有一个named_scope,如下所示: # default product scope only lists available and non-deleted products ::Product.named_scope :active, lambda { |*args| Product.not_deleted.available(args.first).scope(:find) } 最初的named_scope是有道理的。 这里令人困惑的部分是.scope(:find)如何工作。 这显然是调用另一个命名范围(not_deleted),然后应用.scope(:find)。 什么/如何.scope(:find)在这里工作?

使用PostgreSQL使用ActiveRecord准备和执行语句

我试图通过使用ActiveRecord的预准备语句插入值。 但是,每次我尝试: conn = ActiveRecord::Base.connection conn.prepare “SELECT * from sampletable where id = $1” conn.execute 3 在第二个陈述之后,我得到: NoMethodError: undefined method `prepare’ for # 我该怎么办? 我正在运行Rails 3.2.1和Ruby 1.9.2 更新: 我解决了这个问题。 感谢您的回复,但它对PostgreSQL无效。 这样做的方法是: stmt = “SELECT * from sampletable where id = $1 and name = $2” values = [ { value: 1}, { value: “henry” } […]

我可以通过activerecord关系创建activerecord关联(使用祖先gem时)吗?

我在rails项目中使用ancestry gem来创建组的层次结构。 一个组可以属于父组,并且可以有许多子组。 每个组可以拥有许多属于某个组的用户。 该模型如下所示: class Group < ActiveRecord::Base has_ancestry has_many :users end 我希望能够为一个群体的后代获得所有用户,如下所示: class Group < ActiveRecord::Base has_ancestry has_many :users has_many :descendants_users, through: :descendants end 当然,这不起作用。 有什么建议?

是否真的需要validation外键?

我正在使用Ruby on Rails v3.2.2,在发布我之前的问题之后 ,我想知道并理解是否(或不) 显式validation与ActiveRecord::Associations相关的外键是否需要。 例如: class CategoryAssociation ‘article_id’ belongs_to :category, :foreign_key => ‘category_id’ validates :article_id, :presence => true, :numericality => { :only_integer => true } validates :category_id, :presence => true, :numericality => { :only_integer => true } end 真的需要以上validates方法吗? 为什么?

Ruby on Rails – STI的替代品?

我有许多不同的模型(接近20个),它们有一些共同的属性,但在某些程度上也有所不同。 STI起初看起来很有吸引力,但我不知道随着产品的快速发展,各种模型会如何随着时间的推移而发展。 与我们的应用程序很好地平行的是Yelp。 Yelp如何在Rails中管理一些东西? 所有post都有一些共同的属性,如“地址”。 然而,他们在其他方面存在很大差异。 例如,您有餐厅的预订选项,而其他人可能没有。 餐馆还有许多其他属性,如“允许饮酒”,不适用于他人。 使用STI执行此操作将很快失控。 那么下一个最佳选择是什么? 与Postgres的HStore? 我不习惯使用HStore来做任何事情。 HStore解决了一些问题,同时介绍了其他问题,如缺乏数据类型,缺乏参照完整性检查等。我希望有一个可靠的关系数据库作为构建的基础。 所以在Yelp案例中,可能是餐馆模特是我要去的地方。 我已经看过像这里的建议 – http://mediumexposure.com/multiple-table-inheritance-active-record/ ,但是我很乐意做这么多的猴子修补以获得如此常见的东西。 所以我想知道还有其他替代方案(如果有的话)或者我应该咬紧牙关,磨牙并将这些常见属性复制到20个模型中? 我认为我的问题将来自迁移文件而不是代码本身。 例如,如果我将迁移设置为循环遍历表并在表上设置这些属性,那么我是否可以通过使用不同的模型来减轻问题的严重程度? 我是否会忽略一些可能导致大量问题的重要问题,而这些问题将会产生一些单独的模型?