- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 MySQL 5.5 中使用 ActiveRecord 4.2
我有一个名为 Product 的模型。
在控制台中运行 Product.last
- 我在表中获得了第一条记录(2013 年的 id 为 1 和 updated_at)
运行 Product.last(1)
- 我在 2015 年 2 月获得表中 ID 为 (122039) 和 updated_at 的最后一条记录。
我的印象是他们应该返回相同的。为什么 Product.last
会返回 Product.first
的结果?
附言:这是一个开发数据库。所以我销毁它并重新迁移它并重新播种所有数据。不知道这是否重要,但为了加快播种过程(因为我为一些表播种了 100,000 行生产级数据)- 我使用批量插入和 ActiveRecord::Base 从 .sql 文件播种。 connection.execute(sql_from_file)
更新
经过比较(参见下面的 2 个编辑),Product.last 似乎没有执行 order by id desc
,而 Product.last(1) 实际上执行了。有什么想法吗?
编辑 1来自日志:
Product.last (1)
SELECT DISTINCT
`products`.`id`
FROM
`products`
LEFT OUTER JOIN `restrictions`
ON `restrictions`.`id` = `products`.`distribution_restriction_id`
AND `restrictions`.`type` IN ('DistributionRestriction')
LEFT OUTER JOIN `vendors`
ON `vendors`.`id` = `products`.`vendor_id`
LEFT OUTER JOIN `manufacturers`
ON `manufacturers`.`id` = `products`.`manufacturer_id`
LEFT OUTER JOIN `categories`
ON `categories`.`id` = `products`.`category_id`
AND `categories`.`type` IN ('ProductCategory')
LEFT OUTER JOIN `products_restrictions`
ON `products_restrictions`.`product_id` = `products`.`id`
LEFT OUTER JOIN `restrictions` `restrictions_products`
ON `restrictions_products`.`id` = `products_restrictions`.`restriction_id`
AND `restrictions_products`.`type` IN ('ProductRestriction')
LEFT OUTER JOIN `categories` `departments_products`
ON `departments_products`.`id` = `products`.`department_id`
AND `departments_products`.`type` IN ('Department')
LEFT OUTER JOIN `comments`
ON `comments`.`commentable_id` = `products`.`id`
AND `comments`.`commentable_type` = 'Product'
LEFT OUTER JOIN `pictures`
ON `pictures`.`picturable_id` = `products`.`id`
AND `pictures`.`picturable_type` = 'Product'
ORDER BY `products`.`id` DESC
LIMIT 1 ;
SELECT
`products`.`id` AS t0_r0,
`products`.`name` AS t0_r1,
`products`.`client_number` AS t0_r2,
`products`.`client_reference_string` AS t0_r3,
`products`.`client_id` AS t0_r4,
`products`.`vendor_number` AS t0_r5,
`products`.`vendor_reference_string` AS t0_r6,
`products`.`vendor_id` AS t0_r7,
`products`.`manufacturer_number` AS t0_r8,
`products`.`manufacturer_reference_string` AS t0_r9,
`products`.`manufacturer_id` AS t0_r10,
`products`.`reman_number` AS t0_r11,
`products`.`superseded_by_id` AS t0_r12,
`products`.`description` AS t0_r13,
`products`.`category_id` AS t0_r14,
`products`.`first_year_used` AS t0_r15,
`products`.`last_year_used` AS t0_r16,
`products`.`reorder_level` AS t0_r17,
`products`.`minimum_order_quantity` AS t0_r18,
`products`.`total_stock` AS t0_r19,
`products`.`stock_details` AS t0_r20,
`products`.`status` AS t0_r21,
`products`.`discontinued_at` AS t0_r22,
`products`.`currency` AS t0_r23,
`products`.`dealer_exchange_amount_cents` AS t0_r24,
`products`.`dealer_sale_amount_cents` AS t0_r25,
`products`.`retail_exchange_amount_cents` AS t0_r26,
`products`.`retail_sale_amount_cents` AS t0_r27,
`products`.`cost_amount_cents` AS t0_r28,
`products`.`core_charge_amount_cents` AS t0_r29,
`products`.`reman_amount_cents` AS t0_r30,
`products`.`out_of_warranty` AS t0_r31,
`products`.`backordered_till` AS t0_r32,
`products`.`repair_only` AS t0_r33,
`products`.`publicly_visible` AS t0_r34,
`products`.`period_usage` AS t0_r35,
`products`.`distribution_restriction_id` AS t0_r36,
`products`.`department_id` AS t0_r37,
`products`.`stats` AS t0_r38,
`products`.`announcement_date` AS t0_r39,
`products`.`status_with_vendor` AS t0_r40,
`products`.`masterfile_details` AS t0_r41,
`products`.`created_at` AS t0_r42,
`products`.`updated_at` AS t0_r43,
`restrictions`.`id` AS t1_r0,
`restrictions`.`name` AS t1_r1,
`restrictions`.`code` AS t1_r2,
`restrictions`.`description` AS t1_r3,
`restrictions`.`severity` AS t1_r4,
`restrictions`.`begins_at` AS t1_r5,
`restrictions`.`expires_at` AS t1_r6,
`restrictions`.`type` AS t1_r7,
`restrictions`.`created_at` AS t1_r8,
`restrictions`.`updated_at` AS t1_r9,
`vendors`.`id` AS t2_r0,
`vendors`.`name` AS t2_r1,
`vendors`.`code` AS t2_r2,
`vendors`.`created_at` AS t2_r3,
`vendors`.`updated_at` AS t2_r4,
`manufacturers`.`id` AS t3_r0,
`manufacturers`.`name` AS t3_r1,
`manufacturers`.`code` AS t3_r2,
`manufacturers`.`created_at` AS t3_r3,
`manufacturers`.`updated_at` AS t3_r4,
`categories`.`id` AS t4_r0,
`categories`.`name` AS t4_r1,
`categories`.`code` AS t4_r2,
`categories`.`description` AS t4_r3,
`categories`.`type` AS t4_r4,
`categories`.`category_id` AS t4_r5,
`categories`.`created_at` AS t4_r6,
`categories`.`updated_at` AS t4_r7,
`restrictions_products`.`id` AS t5_r0,
`restrictions_products`.`name` AS t5_r1,
`restrictions_products`.`code` AS t5_r2,
`restrictions_products`.`description` AS t5_r3,
`restrictions_products`.`severity` AS t5_r4,
`restrictions_products`.`begins_at` AS t5_r5,
`restrictions_products`.`expires_at` AS t5_r6,
`restrictions_products`.`type` AS t5_r7,
`restrictions_products`.`created_at` AS t5_r8,
`restrictions_products`.`updated_at` AS t5_r9,
`departments_products`.`id` AS t6_r0,
`departments_products`.`name` AS t6_r1,
`departments_products`.`code` AS t6_r2,
`departments_products`.`description` AS t6_r3,
`departments_products`.`type` AS t6_r4,
`departments_products`.`category_id` AS t6_r5,
`departments_products`.`created_at` AS t6_r6,
`departments_products`.`updated_at` AS t6_r7,
`comments`.`id` AS t7_r0,
`comments`.`title` AS t7_r1,
`comments`.`body` AS t7_r2,
`comments`.`author_id` AS t7_r3,
`comments`.`category` AS t7_r4,
`comments`.`commentable_id` AS t7_r5,
`comments`.`commentable_type` AS t7_r6,
`comments`.`created_at` AS t7_r7,
`comments`.`updated_at` AS t7_r8,
`pictures`.`id` AS t8_r0,
`pictures`.`name` AS t8_r1,
`pictures`.`title` AS t8_r2,
`pictures`.`path` AS t8_r3,
`pictures`.`picturable_id` AS t8_r4,
`pictures`.`picturable_type` AS t8_r5,
`pictures`.`category` AS t8_r6,
`pictures`.`created_at` AS t8_r7,
`pictures`.`updated_at` AS t8_r8
FROM
`products`
LEFT OUTER JOIN `restrictions`
ON `restrictions`.`id` = `products`.`distribution_restriction_id`
AND `restrictions`.`type` IN ('DistributionRestriction')
LEFT OUTER JOIN `vendors`
ON `vendors`.`id` = `products`.`vendor_id`
LEFT OUTER JOIN `manufacturers`
ON `manufacturers`.`id` = `products`.`manufacturer_id`
LEFT OUTER JOIN `categories`
ON `categories`.`id` = `products`.`category_id`
AND `categories`.`type` IN ('ProductCategory')
LEFT OUTER JOIN `products_restrictions`
ON `products_restrictions`.`product_id` = `products`.`id`
LEFT OUTER JOIN `restrictions` `restrictions_products`
ON `restrictions_products`.`id` = `products_restrictions`.`restriction_id`
AND `restrictions_products`.`type` IN ('ProductRestriction')
LEFT OUTER JOIN `categories` `departments_products`
ON `departments_products`.`id` = `products`.`department_id`
AND `departments_products`.`type` IN ('Department')
LEFT OUTER JOIN `comments`
ON `comments`.`commentable_id` = `products`.`id`
AND `comments`.`commentable_type` = 'Product'
LEFT OUTER JOIN `pictures`
ON `pictures`.`picturable_id` = `products`.`id`
AND `pictures`.`picturable_type` = 'Product'
WHERE `products`.`id` IN (122039)
ORDER BY `products`.`id` DESC
对比第二个 Product.last
SQL (2.1 ms)
SELECT DISTINCT
`products`.`id`
FROM
`products`
LEFT OUTER JOIN `restrictions`
ON `restrictions`.`id` = `products`.`distribution_restriction_id`
AND `restrictions`.`type` IN ('DistributionRestriction')
LEFT OUTER JOIN `vendors`
ON `vendors`.`id` = `products`.`vendor_id`
LEFT OUTER JOIN `manufacturers`
ON `manufacturers`.`id` = `products`.`manufacturer_id`
LEFT OUTER JOIN `categories`
ON `categories`.`id` = `products`.`category_id`
AND `categories`.`type` IN ('ProductCategory')
LEFT OUTER JOIN `products_restrictions`
ON `products_restrictions`.`product_id` = `products`.`id`
LEFT OUTER JOIN `restrictions` `restrictions_products`
ON `restrictions_products`.`id` = `products_restrictions`.`restriction_id`
AND `restrictions_products`.`type` IN ('ProductRestriction')
LEFT OUTER JOIN `categories` `departments_products`
ON `departments_products`.`id` = `products`.`department_id`
AND `departments_products`.`type` IN ('Department')
LEFT OUTER JOIN `comments`
ON `comments`.`commentable_id` = `products`.`id`
AND `comments`.`commentable_type` = 'Product'
LEFT OUTER JOIN `pictures`
ON `pictures`.`picturable_id` = `products`.`id`
AND `pictures`.`picturable_type` = 'Product'
LIMIT 1 ;
SELECT
`products`.`id` AS t0_r0,
`products`.`name` AS t0_r1,
`products`.`client_number` AS t0_r2,
`products`.`client_reference_string` AS t0_r3,
`products`.`client_id` AS t0_r4,
`products`.`vendor_number` AS t0_r5,
`products`.`vendor_reference_string` AS t0_r6,
`products`.`vendor_id` AS t0_r7,
`products`.`manufacturer_number` AS t0_r8,
`products`.`manufacturer_reference_string` AS t0_r9,
`products`.`manufacturer_id` AS t0_r10,
`products`.`reman_number` AS t0_r11,
`products`.`superseded_by_id` AS t0_r12,
`products`.`description` AS t0_r13,
`products`.`category_id` AS t0_r14,
`products`.`first_year_used` AS t0_r15,
`products`.`last_year_used` AS t0_r16,
`products`.`reorder_level` AS t0_r17,
`products`.`minimum_order_quantity` AS t0_r18,
`products`.`total_stock` AS t0_r19,
`products`.`stock_details` AS t0_r20,
`products`.`status` AS t0_r21,
`products`.`discontinued_at` AS t0_r22,
`products`.`currency` AS t0_r23,
`products`.`dealer_exchange_amount_cents` AS t0_r24,
`products`.`dealer_sale_amount_cents` AS t0_r25,
`products`.`retail_exchange_amount_cents` AS t0_r26,
`products`.`retail_sale_amount_cents` AS t0_r27,
`products`.`cost_amount_cents` AS t0_r28,
`products`.`core_charge_amount_cents` AS t0_r29,
`products`.`reman_amount_cents` AS t0_r30,
`products`.`out_of_warranty` AS t0_r31,
`products`.`backordered_till` AS t0_r32,
`products`.`repair_only` AS t0_r33,
`products`.`publicly_visible` AS t0_r34,
`products`.`period_usage` AS t0_r35,
`products`.`distribution_restriction_id` AS t0_r36,
`products`.`department_id` AS t0_r37,
`products`.`stats` AS t0_r38,
`products`.`announcement_date` AS t0_r39,
`products`.`status_with_vendor` AS t0_r40,
`products`.`masterfile_details` AS t0_r41,
`products`.`created_at` AS t0_r42,
`products`.`updated_at` AS t0_r43,
`restrictions`.`id` AS t1_r0,
`restrictions`.`name` AS t1_r1,
`restrictions`.`code` AS t1_r2,
`restrictions`.`description` AS t1_r3,
`restrictions`.`severity` AS t1_r4,
`restrictions`.`begins_at` AS t1_r5,
`restrictions`.`expires_at` AS t1_r6,
`restrictions`.`type` AS t1_r7,
`restrictions`.`created_at` AS t1_r8,
`restrictions`.`updated_at` AS t1_r9,
`vendors`.`id` AS t2_r0,
`vendors`.`name` AS t2_r1,
`vendors`.`code` AS t2_r2,
`vendors`.`created_at` AS t2_r3,
`vendors`.`updated_at` AS t2_r4,
`manufacturers`.`id` AS t3_r0,
`manufacturers`.`name` AS t3_r1,
`manufacturers`.`code` AS t3_r2,
`manufacturers`.`created_at` AS t3_r3,
`manufacturers`.`updated_at` AS t3_r4,
`categories`.`id` AS t4_r0,
`categories`.`name` AS t4_r1,
`categories`.`code` AS t4_r2,
`categories`.`description` AS t4_r3,
`categories`.`type` AS t4_r4,
`categories`.`category_id` AS t4_r5,
`categories`.`created_at` AS t4_r6,
`categories`.`updated_at` AS t4_r7,
`restrictions_products`.`id` AS t5_r0,
`restrictions_products`.`name` AS t5_r1,
`restrictions_products`.`code` AS t5_r2,
`restrictions_products`.`description` AS t5_r3,
`restrictions_products`.`severity` AS t5_r4,
`restrictions_products`.`begins_at` AS t5_r5,
`restrictions_products`.`expires_at` AS t5_r6,
`restrictions_products`.`type` AS t5_r7,
`restrictions_products`.`created_at` AS t5_r8,
`restrictions_products`.`updated_at` AS t5_r9,
`departments_products`.`id` AS t6_r0,
`departments_products`.`name` AS t6_r1,
`departments_products`.`code` AS t6_r2,
`departments_products`.`description` AS t6_r3,
`departments_products`.`type` AS t6_r4,
`departments_products`.`category_id` AS t6_r5,
`departments_products`.`created_at` AS t6_r6,
`departments_products`.`updated_at` AS t6_r7,
`comments`.`id` AS t7_r0,
`comments`.`title` AS t7_r1,
`comments`.`body` AS t7_r2,
`comments`.`author_id` AS t7_r3,
`comments`.`category` AS t7_r4,
`comments`.`commentable_id` AS t7_r5,
`comments`.`commentable_type` AS t7_r6,
`comments`.`created_at` AS t7_r7,
`comments`.`updated_at` AS t7_r8,
`pictures`.`id` AS t8_r0,
`pictures`.`name` AS t8_r1,
`pictures`.`title` AS t8_r2,
`pictures`.`path` AS t8_r3,
`pictures`.`picturable_id` AS t8_r4,
`pictures`.`picturable_type` AS t8_r5,
`pictures`.`category` AS t8_r6,
`pictures`.`created_at` AS t8_r7,
`pictures`.`updated_at` AS t8_r8
FROM
`products`
LEFT OUTER JOIN `restrictions`
ON `restrictions`.`id` = `products`.`distribution_restriction_id`
AND `restrictions`.`type` IN ('DistributionRestriction')
LEFT OUTER JOIN `vendors`
ON `vendors`.`id` = `products`.`vendor_id`
LEFT OUTER JOIN `manufacturers`
ON `manufacturers`.`id` = `products`.`manufacturer_id`
LEFT OUTER JOIN `categories`
ON `categories`.`id` = `products`.`category_id`
AND `categories`.`type` IN ('ProductCategory')
LEFT OUTER JOIN `products_restrictions`
ON `products_restrictions`.`product_id` = `products`.`id`
LEFT OUTER JOIN `restrictions` `restrictions_products`
ON `restrictions_products`.`id` = `products_restrictions`.`restriction_id`
AND `restrictions_products`.`type` IN ('ProductRestriction')
LEFT OUTER JOIN `categories` `departments_products`
ON `departments_products`.`id` = `products`.`department_id`
AND `departments_products`.`type` IN ('Department')
LEFT OUTER JOIN `comments`
ON `comments`.`commentable_id` = `products`.`id`
AND `comments`.`commentable_type` = 'Product'
LEFT OUTER JOIN `pictures`
ON `pictures`.`picturable_id` = `products`.`id`
AND `pictures`.`picturable_type` = 'Product'
WHERE `products`.`id` IN (1)
** 编辑 2**
default_scope {eager_load([:distribution_restriction, :vendor, :manufacturer, :category, :restrictions, :department, :comments, :pictures])}
最佳答案
ActiveRecord 的 #first
和 #last
没有任何隐含的顺序,它由您的数据库来完成。
在大多数情况下,它们会按主键的顺序返回,但并非总是如此。
最好在您确实需要时明确说明您的订单。
关于mysql - ActiveRecord .last 和 ActiveRecord .last(1) 返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678611/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!