简体   繁体   English

object_id的别称是什么?

[英]What's another name for object_id?

I just realized one of my models has object_id as a column, and that's going to cause prolems. 我刚刚意识到我的一个模型将object_id作为一列,这会引起麻烦。

Any suggestions on possible alternatives for the name object_id ? 关于名称object_id可能替代方案的任何建议?

What is this column supposed to be mapping to? 该列应映射到什么? Is it a foreign key to an objects table? 它是对象表的外键吗?

Figure out what you're really trying to represent. 弄清楚您真正要代表的内容。 It's probably not just any generic thing in the whole world. 这可能不只是全世界的任何普通事物。 (If it is, maybe things is a better name.) (如果是,也许事情是一个更好的名字。)

If you're working under constraints and you absolutely must have that object_id column, you could still refer to it directly with attributes[:object_id] and bypass Rails's magic methods. 如果您在约束条件下工作,并且绝对必须有该object_id列,则仍可以直接使用attributes[:object_id]引用它,并绕过Rails的magic方法。

As a last resort, you could overwrite the method with your own #object_id method that simply returns that attribute from your database (this is what Rails did with the #id method). 作为最后的选择,您可以使用自己的#object_id方法覆盖该方法,该方法#object_id数据库中返回该属性(这是Rails使用#id方法所做的#id )。 I can't think of anything that would definitely break off the top of my head, but it's probably not a great idea. 我想不出任何肯定会打破我头脑的东西,但这可能不是一个好主意。 The object ID is used for a lot of miscellaneous things, so you may get strange behavior if you do object comparisons, use your object as a hash key, etc. 对象ID用于许多其他事情,因此如果进行对象比较,将对象用作哈希键等,您可能会得到奇怪的行为。

You don't need to use object_id at in your model. 您无需在模型中使用object_id And there should be no column named object_id in the database. 并且数据库中不应有名为object_id列。

object_id is just a default methods that all (except BasicObject in Ruby 1.9) objects have (see docs ). object_id只是所有对象(Ruby 1.9中的BasicObject除外)都具有的默认方法(请参阅docs )。

Returns an integer identifier for obj. 返回obj的整数标识符。 The same number will be returned on all calls to id for a given object, and no two active objects will share an id. 给定对象的所有id调用都将返回相同的编号,并且没有两个活动对象共享一个id。 Replaces the deprecated Object#id . 替换不推荐使用的Object#id

2.object_id # 5 or anything, but the same
2.id # NoMethodError: undefined method `id' for 2:Fixnum
2.object_id # 5
"anything has object_id".object_id # 22522080
"anything has object_id".object_id # 22447200 - string has different object_id because it's a new instance everytime

So, just use id to access database identifier of ActiveRecord classes. 因此,只需使用id即可访问ActiveRecord类的数据库标识符。 That is the one created by Ruby On Rails for model objects. 这是Ruby On Rails为模型对象创建的对象。


OR if you do need to have the column in the database called object_id then you can create a method on the ActiveRecord model like this: 或者,如果确实需要在数据库中有一个名为object_id的列,则可以在ActiveRecord模型上创建一个方法,如下所示:

def general_id
  read_attribute(:object_id)
end

I'm kind of surprised that Rails doesn't create __object_id__ as a reference to the original form of object_id , like send has a Ruby variant called __send__ . 我很惊讶Rails不会创建__object_id__作为对object_id原始形式的引用,就像send具有一个名为__send__的Ruby变体__send__

Edit: The Ruby method __id__ appears to be the __send__ equivalent for object_id . 编辑: Ruby方法__id__似乎是object_id__send__等效项。 It may be safe to use object_id as a method for your foreign key, or it may not. 使用object_id作为外键的方法可能是安全的,也可能不是。 I don't actually use Rails in my current job. 我目前的工作实际上并不使用Rails。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM