Tag: activerecord

Rails 3.2(ActiveRecord)关联顺序与急切的加载和排序

我在rails中遇到了这种行为,我在文档中似乎找不到。 看起来,如果您将ActiveRecord查询复杂化到将基础SQL集成到“复杂”查询中(参见下文),则它不会遵循关联上指定的排序。 这是我的示例模型: class Article :name attr_accessible :title, :user_id, :user end 如果我执行“简单”查询,我可以获取按名称排序的相关类别: > Article.includes(:categories).first.categories.map &:name Article Load (0.1ms) SELECT “articles”.* FROM “articles” LIMIT 1 Category Load (0.2ms) SELECT “categories”.* FROM “categories” WHERE “categories”.”article_id” IN (11) ORDER BY name => [“category 3059”, “category 3212”, “category 3240”, “category 3651”, “category 4371”, “category 5243”, “category 6176”, “category 6235”, […]

activerecord -3.1.11出错无法找到表(ActiveRecord :: Statement Invalid)

在将我的应用程序推送到heroku之前,我在生产模式下测试了我的应用程序,如果它处于开发模式,它可以正常工作,但在生产模式下我得到以下错误activerecord -3.1.11无法找到表格(ActiveRecord :: Statement无效) )。 如何解决此错误? 我确信表’页’存在。 C:\Sites\ecozap>rails s -e production => Booting WEBrick => Rails 3.1.11 application starting in production on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server Exiting C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.1.11/lib/active_record/connection_ad apters/sqlite_adapter.rb:450:in `table_structure’: Could not find table ‘pages’ (ActiveRecord::Statement Invalid) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.1.11/lib/active_record/ connection_adapters/sqlite_adapter.rb:333:in `columns’ from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.1.11/lib/active_record/ connection_adapters/abstract/connection_pool.rb:95:in `block (2 levels) […]

在IRB上需要ActiveRecord – Ruby(NO Rails)

如何在IRB会话上加载ActiveRecord ? 我有以下内容 # config/app.rb require ‘active_record’ ActiveRecord::Base.establish_connection( adapter: ‘sqlite3’, database: ‘db/mydb.sqlite3′ ) 但是当我启动IRB并尝试加载它时 irb#1(main):001:0> require config/application.rb 我明白了 NameError: undefined local variable or method `config’ for main:Object Did you mean? conf 我希望能够与IRB中的ActiveRecord对象进行交互。 我不使用Rails,只使用ActiveRecord。 谢谢

Rails仅迭代与对象关联的持久记录

我的show动作中有这样的东西: def show @blog = Blog.find(params[:id]) @new_comment = @blog.comments.build end 现在在视图中我做了两件事: 我为Comment渲染_form.html.erb ,以便用户可以向此博客添加评论 我希望能够遍历此博客的现有评论,以便显示它们。 我遇到了2号问题。 问题在于,当我用以下方法迭代相关的comments时: 在迭代关联的博客时,它抓住了@new_comment 。 如何排除那些内置但尚未持久化的@new_comment ? 如果像@blog.comments.each_with_id这样的东西工作,甚至@blog.comments.persisted.each 。 最后,我只想迭代与此blog相关的持久性comments 。 我宁愿不必嵌套条件,询问评论是否persisted? 。 我希望我的当前情况有一个迭代器可以抓住持久化的记录。

如何在Ruby中生成正则表达式字符串匹配的百分比?

我正在尝试构建一个简单的方法来查看数据库中的大约100个条目以获取姓氏,并拉出所有匹配超过特定字母百分比的条目。 我目前的做法是: 将数据库中的所有100个条目拉入数组 在执行以下操作时迭代它们 将姓氏拆分为一个字母数组 从另一个数组中减去该数组,该数组包含我要匹配的名称的字母,只留下不匹配的字母。 取结果的大小,除以步骤3中数组的原始大小,得到一个百分比。 如果百分比高于预定义阈值,请将该数据库对象推送到结果数组中。 这有效,但我觉得必须有一些很酷的ruby / regex / active记录方法来更有效地做到这一点。 我google了很多但找不到任何东西。

Rails ActiveRecord ::关系错误

考虑将变量作为值传递给ActiveRcord查询键/列? login = Login.where(“email_address” => “geo@bostj.com”,”active” => 1).select(“id”) => [#] login = Login.where(“email_address” => “geo@bostj.com”,”active” => 1).select(“id”).class => ActiveRecord::Relation admin = Admin.where(“login_id”=>login).exists? Heeeelp

将Postgres查询转换为Rails ActiveRecord?

是否可以用Rail的ActiveRecord格式编写以下查询? 我已经用arel尝试了十亿种不同的方式,但无法获得相同的结果。 SELECT topic_id, count(*) as total FROM questions_topics WHERE question_id IN ( SELECT id FROM questions WHERE user_id = 1000 UNION ALL SELECT questions.id FROM questions JOIN answers ON (questions.id = answers.question_id) WHERE answers.user_id = 1000 ) GROUP BY topic_id ORDER BY total DESC;

Ruby on Rails:数据没有保存ActiveRecord

我是Ruby on Rails的新手,我一直在努力开发一个简单的博客。 但是,当我尝试保存新post时,页面会重新加载新页面,而不会保存任何数据。 但是数据存在于URI中。 这是我的控制器: class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] # GET /posts # GET /posts.json def index @posts = Post.all end # GET /posts/1 # GET /posts/1.json def show end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit end # POST […]

使用内部联接Rails ActiveRecord查询

我有两个表,我正在尝试进行内部联接。 一个是主键为id的users表。 另一个表是其中user_id是外键的bars 。 bars还有一个名为foo_id的列,其中food_id是foos表的外键。 我正在尝试将ActiveRecord查询放在一起,我可以选择在N天前或之前创建的所有用户,并且没有任何foos ,其中bars.foo_id等于特定的id。 我尝试过这样的事情: users = User.where(“users.created_at <= ?", 50.days.ago).joins(:bars).where("bars.foo_id != 5") 此查询字段超过30,000个结果,这是不正确的,因为Users表只有12,000行。 我到底错在了什么?

Ruby on Rails模型。 为什么范围为1的十进制{2,1}允许小数点后的数字?

尽管定义了它的精度和范围,但是在小数点后接受过多位数的表存在问题。 rails generate model Hotel name:string ‘rating:decimal{2,1}’ class CreateHotels < ActiveRecord::Migration def change create_table :hotels do |t| t.string :name t.decimal :rating, precision: 2, scale: 1 t.timestamps end end end 但是,我能够做到以下几点。 Hotel.create!(name: “The Holiday Inn”, rating: 3.75) 另外,我有一个房间桌(房间模型),有 t.decimal :rate, precision: 5, scale: 2 #this holds the room’s nightly rate 我在这列中输入了99.99,但最终将其存储为99.98999999 .. 为什么我有这两个小数问题? 如果我已经定义了我的范围,为什么我可以输入比我定义的范围更多的范围?