Tag: enums

ActiveRecord :: Enum – PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:

我有一个奇怪的错误,并希望有人能指出我正确的方向。 我有一个名为Organizations的模型,以及一个名为department的属性,请参阅下面的模式摘录: t.integer “department”, default: 0 在我的模型里面已经为这个属性定义了我的枚举,因为我正在使用ActiveRecord :: Enum ,如下所示: enum department: [:conferences, :design_teams, :services, :clubs, :events, :communications] 但是当我查询JobPosting.joins(job: :organization).where(organizations: { department: ‘conferences’ })我收到的错误是: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “conferences” 仅供参考:一个组织有很多乔布斯,乔布斯有很多JobPostings。 但是,当我查询Organization.where(department: ‘conferences’)它可以工作。 任何帮助将不胜感激。

工厂女孩与枚举和协会

我有一个使用关联和枚举属性的模型。 class ProjectItem < ActiveRecord::Base belongs_to :project enum status: {open: 0, pending: 1, completed: 2} 当我在带有关联的模型的创建操作上运行测试时,我使用build(:model_name).attributes如下所示: it “creates a new ProjectItem” do expect { post :create, document_project_item: build(:project_item).attributes }.to change(ProjectItem, :count).by(1) end 这是失败的,我发现这个问题线程解释了为什么它不起作用 。 根据评论,我能够确定在具有enum属性但没有关联的表上,按预期方式使用attributes_for(:model_name) 。 问题主题似乎没有建议解决方法,但我承认我不明白FactoryGirl方法在幕后所做的100%。 这是工厂: factory :project_item do project name { Faker::Company.bs } description { Faker::Lorem.paragraph } status :open due { Faker::Date.between(2.days.ago, […]

基于枚举值的自定义顺序

我为角色定义了Enum: enum role: {ordinary: 0, manager: 1, admin: 2} 我想按以下顺序订购一组对象: admin (first all admins) ordinary (then all ordinaries) manager (and lastly all managers) 这有可能吗?

如何使用太阳黑子在Rails中调整枚举?

我正在尝试使用Sunspot(Rails Solr gem)来使用我在模型中声明的枚举来调整结果范围。 我模型的相关部分如下所示: searchable do text :tag_list boolean :approved integer :perspective time :created_at end enum perspective: [ :not_applicable, :front, :side_front, :side, :side_back, :back, :above, :below ] 我控制器中的搜索块如下所示: def index //skip scoping if perspective is nil params[:perspective] ||= [] @search = Upload.search do with :approved, true with :perspective, params[:perspective] fulltext params[:tag] fulltext params[:search] do minimum_match […]

使用rspec检查错误的枚举

有谁知道如何在rspec中检查无效的枚举响应? 我可以检查以确保枚举不是没有错误,但是当我检查确保坏的枚举值不起作用时,我收到错误。 这些是两个规格: it ‘is not valid without question type’ do expect(build(:question, question_type: nil)).to have(1).errors_on(:question_type) end it ‘is not valid with a bad question type’ do expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type) end 这就是我的模型: class Question < ActiveRecord::Base enum question_type: [ :multiple_choice ] validates :question_type, presence: true end 这是错误: Failure/Error: expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type) ArgumentError: ‘telepathy’ is […]