Tag: activerecord

预加载与动态条件的关联

我有Place模型和Event模型。 地方可以 在特定日期举办活动。 如何在没有N + 1查询问题的特定日期设置我的关联和查找器来加载所有地方,包括(急切加载)他们的事件? 我尝试过的: class Place has_many :events end Place.all.preload(:events).where(“events.start_date > ‘#{time_in_the_future}'”) #ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table “events”. Place.all.includes(:events).where(“events.start_date > ‘#{time_in_the_future}'”).references(:event) # only loads places that have an event at the specific date and not all places including their events (if there are any events). 我成功地想出了一个能够做我想要的但不是动态的关联(不接受参数) class Place has_many […]

Rails:使用“attributes”方法将属性从对象复制到另一个对象

让模特Quote有属性[price, description] 让模型Invoice有属性[price, description, priority] 让我们使用属性{price: 10, description: ‘lamp’, priority: 10}从Model Invoice为对象invoice invoice = {price: 10, description: ‘lamp’, priority: 10} 假设我想将invoice属性复制到新quote 。 quote = Quote.new(invoice.attributes) 这引发了一个错误,即模型Quote中不存在priority 。 如何将invoice属性复制到新quote但只复制quote可以接受的属性?

HOWTO在Ruby on rails上按平衡对项目进行排名

我正在实施一个投注系统,每个用户都有一个余额,我如何使用activerecord方法找到用户的排名? 谢谢你的帮助。

Rails上build和new有什么区别?

任何人都可以告诉我在Rails上构建和新命令之间的区别是什么?

通过Rails中的belongs_to关系查询记录

我有一个活动模型,他们属于一个位置 如何选择location.country = Australia的所有活动? (例如) 我可以在范围内执行此操作吗?

ActiveRecord :: AdapterNotSpecified数据库配置未指定适配器

当我使用heroku打开我的web应用程序工作正常,但当我使用rails(localhost)时,我遇到了这个错误: ActiveRecord::AdapterNotSpecified database configuration does not specify adapter 为什么是这样? 这是我的database.yml # PostgreSQL. Versions 8.2 and up are supported. # # Install the pg driver: # gem install pg # On OS X with Homebrew: # gem install pg — –with-pg-config=/usr/local/bin/pg_config # On OS X with MacPorts: # gem install pg — –with-pg-config=/opt/local/lib/postgresql84/bin/pg_config # On Windows: […]

为什么Rails的“errors.full_messages”不替换属性和消息变量?

我刚刚创建的rails模型有一个奇怪的问题。 以下是我的validation: validates_presence_of :from_name, :message => ‘Please provide a from name.’ validates_presence_of :from_email validates_presence_of :giftition_plan_id 我在使用errors.full_messages以及f.error_messages中的errors.full_messages遇到问题: g = Giftition.create g.errors.first => [“from_name”, “Please provide a from name.”] >> g.errors.full_messages => [“{{attribute}} {{message}}”, “{{attribute}} {{message}}”, “{{attribute}} {{message}}”] 我刚收到”{{attribute}} {{message}}” 。 有任何想法吗? 更新:我已经卸载了rails 3以及随之安装的所有gem,这使问题消失了。 虽然这不是一个修复…我仍然希望安装rails 3。 更新:听起来像升级到2.3.9修复了问题。 不幸的是,我现在已经放弃了,但将来的某个时候我会尝试。

在Rails应用程序中的查询运行时更改表名

我在Apache + mod_passenger上有一个胖的多租户Rails应用程序,它从PostgreSQL表中输出产品价格,如下所示: Table “public.products” Column | Type id | bigint name | character varying(100) price | numeric(8,2) 然后在products.rb我有…… class Product < PostgresDatabase self.table_name = "products" # … yadda yadda end 我想要的是以非常具体的方式对“产品”表进行分区,以便最终为每个租户提供类似product_TENANT-ID的内容(基本上是主要产品表的视图,但这是另一个故事),并且能够像这样查询: Products.for_tenant(TENANT-ID).where(:name => “My product”)…… 我想我可以创建一个方法: class Product < PostgresDatabase self.table_name = "products" # … yadda yadda def for_tenant(tid) self.table_name = "products_" + tid.to_s […]

从ActiveRecord获得排名

如果User有积分,我如何获得用户排名,假设标准定位: require ‘active_record’ class User [, , , ] User.find_by_id(2).rank # => 1 User.find_by_id(3).rank # => 3 User.find_by_id(1).rank # => 3 我得到了明显的印象,这将是数据库密集型。 我并不太担心,但显然需要较少处理能力的解决方案才是赢家! 如果给予具有相同数量的点的两个用户相同的等级太复杂,我准备接受上面的用户1可以是等级3,而用户3可以是等级4。 编辑: 我实际上是在一个单独的课堂上分享gmy分数 – 因为每个事件都需要一定数量的分数。 class Event < ActiveRecord::Base belongs_to :user end class User ?’,points]) + 1 end end 关于如何使这更简洁的任何想法? 提前致谢

Rails用可选参数搜索?

当搜索可以提供许多可选参数(如ID,Zip,City和State)时,如何在数据库上进行搜索? 这些可以具有值或完全空白。 我该怎么做这样的rails查询?