Rails的ActiveRecord的 - 是有没有办法没有ID执行对表的操作? [英] Rails ActiveRecord - is there a way to perform operations on tables without an id?

查看:109
本文介绍了Rails的ActiveRecord的 - 是有没有办法没有ID执行对表的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个行和两个表columns-

I have a table with one row and two columns-

int 'version', datetime 'updated'

有一个Rails的ActiveRecord的方式来获取和设置数据在这些列? 没有ID列。

Is there a Rails ActiveRecord way to get and set the data in these columns? There is no id column.

我用这个表来跟踪其他表的查询的版本。另一个表的每个查询后版本列递增,并更新列设置与当前日期时间

I'm using this table to track versions of queries of other tables. After each query of another table the version column is incremented and the updated column is set with current datetime.

推荐答案

没有prevent你使用它的ActiveRecord没有ID:

Nothing prevent you to use it in ActiveRecord without ID :

迁移包括:

create_table :posts, :id => false do |t|
  t.integer :version
  t.datetime :updated
end

查询:

>> Post.create(:version => 1, :updated => Time.now)
=> #<Post version: 1, updated: "2009-05-05 19:24:31">
>> Post.all
=> [#<Post version: 1, updated: "2009-05-05 19:24:31">]
>> Post.all(:conditions => { :version => 1 })
=> [#<Post version: 1, updated: "2009-05-05 19:24:31">]

日志报告这些SQL请求:

The log report these SQL requests :

CREATE TABLE "posts" ("version" integer, "updated" datetime) ;
INSERT INTO "posts" ("version", "updated") VALUES(1, '2009-05-05 19:24:31');
SELECT * FROM "posts" 
SELECT * FROM "posts" WHERE ("posts"."version" = 1)

希望帮助

这篇关于Rails的ActiveRecord的 - 是有没有办法没有ID执行对表的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆