Guest User

Untitled

a guest
Dec 23rd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. set_primary_key
  2. This is a method that you can use within a model to override the Active Record assumption that each table has a primary key labeled id. You may also provide a block rather than a pri- mary key label, and the result of the block will be the value Active Record attempts to use as the primary key.
  3. In the following example, we override the Active Record assumptions for two models. In both models, Active Record would normally assume each table has a primary key in a field labeled id. However, in the first model, we say to use account_id as the primary key, and in the second model, we use a block to assign a primary key of comments_id:
  4. # Using the set_table_name method
  5. require 'rubygems'
  6. require_gem 'activerecord'
  7. ActiveRecord::Base.establish_connection(:adapter => "sqlserver",
  8. :host => "localhost", :username => "sa", :password => "", :database => "testdb")
  9. class Account < ActiveRecord::Base
  10. set_primary_key "account_id"
  11. end
  12. puts Account.find(1).inspect
  13. class Comment < ActiveRecord::Base
  14. set_primary_key {
  15. d = ["comm","ents","_id"]
  16. d.join }
  17. end
  18. puts Comment.find(1).inspect
Add Comment
Please, Sign In to add comment