Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set_primary_key
- 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.
- 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:
- # Using the set_table_name method
- require 'rubygems'
- require_gem 'activerecord'
- ActiveRecord::Base.establish_connection(:adapter => "sqlserver",
- :host => "localhost", :username => "sa", :password => "", :database => "testdb")
- class Account < ActiveRecord::Base
- set_primary_key "account_id"
- end
- puts Account.find(1).inspect
- class Comment < ActiveRecord::Base
- set_primary_key {
- d = ["comm","ents","_id"]
- d.join }
- end
- puts Comment.find(1).inspect
Add Comment
Please, Sign In to add comment