草庐IT

relational-databases

全部标签

ruby - Heroku 上没有这样的文件 'config/database.yml'

我的应用是一个RubyRack应用。当我的Heroku应用程序启动时它中断了,因为/app/config.ru:8:in`read':Nosuchfileordirectory-config/database.yml(Errno::ENOENT)为什么会这样?我知道Heroku是为了创建这个文件https://devcenter.heroku.com/articles/cedar-migrationThedatabasecredentialswillstillbeconfiguredautomatically:atslugcompiletime,aconfig/database.yml

ruby-on-rails - 可以在 ruby​​ on rails 中使用 "database first"方法吗?

是否可以在GUImysql管理员中创建数据库模式,然后基于该模式创建(或更新)模型? 最佳答案 这就是迁移的目的。学习DSL后,编写迁移(尤其是使用内置生成器)比摆弄GUI来创建表要容易得多。查看migrationsguide了解详细信息。ActiveRecord已经根据数据库模式自动完成了很多工作,例如根据列添加getter和setter。我不确定您还想要什么,所以我不知道如何更具体地帮助您。 关于ruby-on-rails-可以在ruby​​onrails中使用"databasefi

ruby-on-rails - RoR 从 ActiveRecord::Relation 中获取属性

我是RoR的新手。请告诉我如何从ActiveRecord::Relation获取属性?例如我写:@user=User.where(code:123)接下来我要获取属性idid=@user.id但是这个方法行不通。提前致谢 最佳答案 当使用.where时,它会给你一个activerecordrelation所以你不能直接在它上面找到id因为它是一个关系而不是一个单一的模型对象。修复:你可以做到@user=User.where(code:123).first或您可以使用dynamicfinders@user=User.find_by_c

ruby-on-rails - ActiveRecord::Relation.concat 在 Rails 5 中失败

愚蠢的问题,但我不确定为什么这在Rails4.2中有效,但在Rails5.2中无效。FamilyCharacteristic.where(family_id:@user.family_ids).concat(@user.characteristics)规范在5.2中失败:Failure/Error:FamilyCharacteristic.where(family_id:@user.family_ids).concat(@user.characteristics)NoMethodError:undefinedmethod`concat'for#Didyoumean?countconca

ruby - Rails,防止 Model.scoped 的弃用警告,找到(:all) and relation #all?

我有通过但显示的测试$rspecspec/event_calendar_spec.rb......DEPRECATIONWARNING:Model.scopedisdeprecated.PleaseuseModel.allinstead.(calledfromevents_for_date_rangeat/home/durrantm/Dropbox/96_2013/work/code/ruby/event_calendar/lib/event_calendar.rb:52)DEPRECATIONWARNING:Calling#find(:all)isdeprecated.Pleasec

ruby-on-rails - 使用 ActiveRecord::Relation 时的 RSpec 匹配器

所以这是我要测试的方法:defself.by_letter(letter)where("lastnameLIKE?","#{letter}%").order(:lastname)end这里有个小问题,#{letter}后面的百分号到底是做什么用的?与格式化有关?这是测试该方法的规范的一部分:context'method"by_letter"'doit'returnsandorderedlistbyletter'dotheon=Contact.create!(firstname:"Theon",lastname:"Greyjoy",email:"tgreyjoy@ironprice.co

ruby - Rails 事件记录 : Calling Build Method Should Not Save To Database Before Calling Save Method

我有一个简单的用户模型classUser还有一个简单的user_profile模型classUserProfile问题是当我调用下面的构建方法时,没有调用保存方法,我最终在数据库中得到了一条新记录(如果它通过了验证)classUserProfilesController任何人都知道发生了什么事。这个方法的定义如下,但它仍然为我保存build_association(attributes={})Returnsanewobjectoftheassociatedtypethathasbeeninstantiatedwithattributesandlinkedtothisobjectthro

ruby-on-rails - 在 Ruby on Rails 中如何通过键访问对象的 (ActiveRecord::Relation) 值?

tl;dr如何使用对象的键获取对应的值?我不明白为什么Atag.where(tag:'brand')给了我一个我称之为对象的东西,因为没有更好的术语:#]>但我很难访问键:id的相应值。Atag.where(tag:'brand').id和Atag.where(tag:'brand')[:id]和Atag.where(tag:'brand')(:id)所有都抛出错误,而在这种情况下,我只是想返回整数1。我似乎无法使用ruby​​,也无法通过我的google搜索技能(或缺乏)找到这个基本问题的简洁答案。谢谢 最佳答案 来自OdinPr

ruby - 从 ActiveRecord 迁移中读取 database.yml

如何在ActiveRecord迁移中访问database.yml的内容?具体来说,我需要要迁移的数据库的database键的值。 最佳答案 db=YAML.load_file("#{RAILS_ROOT}/config/database.yml")[RAILS_ENV]['database'] 关于ruby-从ActiveRecord迁移中读取database.yml,我们在StackOverflow上找到一个类似的问题: https://stackoverf

ruby - database.yml 在 Rails 4 的 ActiveRecord 中加载到哪里?

Rails应用程序的config/database.yml加载了ActiveRecord代码库中的哪一行(或方法)?(我正在专门查看4.0.5,但如果有人有关于>=4.0.5的任何信息,那将很有启发性)? 最佳答案 它在Railties中,特别是在文件railties/lib/rails/application/configuration.rb的第101-116行(对于Rails4.0.5):https://github.com/rails/rails/blob/v4.0.5/railties/lib/rails/applicati