Tag: activerecord

无法加载此类文件 – 1.9 / bcrypt_ext(LoadError)

嘿,当我尝试启动rails服务器时,我得到以下错误 有任何想法吗? 目前还没有可行的解决方案…… 顺便说一句,我运行Windows 8 ……可能是问题所在:D C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require’: cannot load such file — 1.9/bcry pt_ext (LoadError) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bcrypt-ruby-3.1.0-x86-mingw32/lib/bcrypt_ext.rb:2:in `’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bcrypt-ruby-3.1.0-x86-mingw32/lib/bcrypt.rb:12:in `’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) […]

ActiveModel方法属性“_was”用于什么?

在控制台中使用自动完成时,我经常会在我的属性后面看到“ _was ”。 但我找不到任何文档或最佳实践用法。 它做了什么以及如何使用它? 示例: user.fname的方法为user.fname_was 使用source_location,我已将其跟踪到:active_model / attribute_methods.rb“,第296行,但没有任何具体内容。

Rails:在每个请求上切换连接但保留连接池

在我们的Rails应用程序中,我们需要使用不同的数据库,具体取决于请求的子域(每个国家/地区的DB不同)。 现在我们正在做类似于这个问题的建议。 也就是说,在每个请求上调用ActiveRecord::Base.establish_connection 。 但似乎 ActiveRecord::Base.establish_connection会丢弃当前连接池,并在每次调用时建立新连接。 我做了这个快速基准测试,看看每次调用establish_connection和建立连接之间是否有任何显着差异: require ‘benchmark/ips’ $config = Rails.configuration.database_configuration[Rails.env] $db1_config = $config.dup.update(‘database’ => ‘db1’) $db2_config = $config.dup.update(‘database’ => ‘db2’) # Method 1: call establish_connection on each “request”. Benchmark.ips do |r| r.report(‘establish_connection:’) do # Simulate two requests, one for each DB. ActiveRecord::Base.establish_connection($db1_config) MyModel.count # A little query to force the DB connection to […]

将@object传递给rails部分渲染

我有一个部分: 的简档/ _show.html.erb’ 包含类似的代码 我试图渲染部分,但我不知道如何通过@profile。 我尝试使用本地但显然它在我的部分而不是’@profile’上设置’profile’。 ‘profiles/show’, :locals => {:profile => @app.profile} %> 无论如何将它作为@object而不是对象传递,还是以这种方式设计?

多态关联在同一模型上具有多个关联

我对我所获得的多态关联感到有些困惑。 我需要一个文章模型来获得标题图像和许多图像,但我希望有一个图像模型。 更令人困惑的是,Image模型具有多态性(允许其他资源拥有许多图像)。 我在我的文章模型中使用此关联: class Article :imageable has_many :images, :as => :imageable end 这可能吗? 谢谢。

覆盖database.yml的策略?

在我的环境中,部署服务器具有database.yml中的大量连接信息。 也就是说,他们知道它们是开发服务器,测试服务器还是生产服务器,并且知道它们各自的数据库连接信息。 我可以将此信息封装在Server类中,以便我可以检索信息: Server[“environment”] #=> production Server[“db_host”] #=> db5.example.com Server[“db_password”] #=> [a decrypted password] 等等。 我想部署一个Rails应用程序,并根据服务器设置进行自动配置。 做这个的最好方式是什么? 一种方法是在我的database.yml中使用Erb: : adapter: oracle_enhanced host: username: db_user password: 我不是太兴奋这样做,但它会工作。 在这种情况下,我将在哪里放置定义Server类的’server.rb’ – 在yml中需要它? 在ActiveRecord加载database.yml之后加载app / initializers。 另一种可能的解决方案是以某种方式覆盖railties的数据库初始化器: # File railties/lib/initializer.rb, line 903 def database_configuration require ‘erb’ YAML::load(ERB.new(IO.read(database_configuration_file)).result) end 仅在以下情况下调用上述内容:在config.frameworks中定义了active_record。 我不确定如何在Rails启动序列中尽早覆盖这一点。 也许第三个选项是从config.frameworks中删除 :active_record,然后在应用程序初始化程序中创建连接? 我担心这可能会产生很多意想不到的副作用。 我希望有一些简单而明显的东西我没有找到,例如ActiveRecordfunction允许我选择退出database.yml并以编程方式提供备用配置。

Rails 3使用SQL IN和SQL OR运算符进行ActiveRecord查询

我正在使用“where”语法编写Rails 3 ActiveRecord查询,该语法使用SQL IN和SQL OR运算符,并且无法弄清楚如何将它们一起使用。 此代码有效(在我的用户模型中): Question.where(:user_id => self.friends.ids) #note: self.friends.ids returns an array of integers 但是这段代码 Question.where(:user_id => self.friends.ids OR :target => self.friends.usernames) 返回此错误 syntax error, unexpected tCONSTANT, expecting ‘)’ …user_id => self.friends.ids OR :target => self.friends.usern… 知道如何在Rails中编写它,或者只是原始SQL查询应该是什么?

Rails 3validation包含何时使用find(如何proc或lambda)

我有一个有CURRENCY和COUNTRY表的项目。 有一个PRICE模型需要有效的货币和国家代码,所以我有以下validation: validates :currency_code, :presence => true, :inclusion => { :in => Currency.all_codes } validates :country_code, :presence => true, :inclusion => { :in => Country.all_codes } all_codes方法返回仅包含货币或国家/地区代码的数组。 只要没有代码添加到表中,这样就可以正常工作。 你会怎么写这个,以便Currency.all_codes的结果是Proc或lambda内? 我尝试了Proc.new {Currency.all_codes} – 然后得到一个错误,该对象没有响应包含?

Ruby中DateTime的毫秒分辨率

我有一个像2012-01-01T01:02:03.456这样的字符串,我使用ActiveRecord存储在Postgres数据库TIMESTAMP中。 不幸的是,Ruby似乎砍掉了毫秒: ruby-1.9.3-rc1 :078 > ‘2012-12-31T01:01:01.232323+3’.to_datetime => Mon, 31 Dec 2012 01:01:01 +0300 Postgrs支持微秒级分辨率。 如何才能相应地保存我的时间戳? 我需要至少毫秒的分辨率。 (PS是的,我可以在postgres中以毫秒整数列进行破解;这种方式会破坏ActiveRecord的整个目的。) 更新: 非常有用的响应表明,Ruby的DateTime并没有缩短毫秒数; 使用#to_f显示它。 但是,做: m.happened_at = ‘2012-01-01T00:00:00.32323’.to_datetime m.save! m.reload m.happened_at.to_f 是否丢弃毫秒。 现在,有趣的是,在Rails和Postgres中, created_at确实显示毫秒。 但其他时间戳字段(如上面的happen_at)则没有。 (也许Rails对created_at使用NOW()函数而不是传入DateTime)。 这导致了我的终极问题: 如何让ActiveRecord在时间戳字段上保留毫秒分辨率?

为什么Rails忽略(伪)嵌套事务中的回滚?

根据文档ActiveRecord :: Transactions :: ClassMethods ,非新嵌套事务将忽略回滚。 来自文档: User.transaction do User.create(username: ‘Kotori’) User.transaction do User.create(username: ‘Nemu’) raise ActiveRecord::Rollback end end raise ActiveRecord::Rollback被忽略,因为它在子事务中(或者更确切地说,它仍然在父事务中而不是它自己的事务中)。 我不明白为什么两个都会忽略回滚调用? 我可以看到,因为子’事务’实际上不是一个事务,它不会回滚’Nemu’块,但为什么它不会触发父事件的回滚? 子事务是否以某种方式隐藏了回滚? 换句话说,为什么似乎无法从嵌套子项中回滚父事务?