The following example
shows this in action:
# Setting the primary_key_prefix_type
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
class Account < ActiveRecord::Base
end
a = Account.find(1)
#=> executes SQL equal to "Select * from accounts where account_id = 1"
?– Note It??™s important to notice that the class name is used without the help of the Active Support Inflector
class for this prefix. That is, we define our model as an Account model that maps to an accounts table (the
Active Support Inflector class helps us to figure out the proper table name that our code should look for),
but within that table, we expect to have a primary key of account_id rather than accounts_id. If you think
about a record and how it relates to a single instance of a class, this should seem like a reasonable deduction
for our code to make. If our primary key does not follow this design, we would need to use the set_primary_
key method within each model rather than just setting this attribute.
It??™s quite common to see primary keys defined as the table name and the string ID (sometimes
with an underscore, sometimes without).
Pages:
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377