We must use a composite key to ensure unique primary keys regardless
of which server an order is created on. First, we install the gem:
gem install composite_primary_keys
Then, we have to require this library in our application. From Rails, we can include
this statement at the end of our environment.rb:
require 'composite_primary_keys'
The next step is to call the set_primary_keys(*keys) method to inform ActiveRecord
that we will be using composite keys:
class Order < ActiveRecord::Base
set_primary_keys :node_id, :order_id
end
After setting up the composite key, most ActiveRecord operations take place as
usual, with the exception that primary keys are now represented by an array rather
than an integer.
Order.primary_key # => [:node_id, :order_id]
Order.primary_key.to_s # => "node_id,order_id"
Order.find 1, 5 # => #
"1",
"order_id"=>"5"}>
Even associations work normally; you only have to specify the foreign key explicitly
on both sides of the association. To demonstrate this, we can add a LineItem model
that belongs to a corresponding Order.
class Order < ActiveRecord::Base
set_primary_keys :node_id, :order_id
has_many :line_items, :foreign_key => [:order_node_id, :order_id]
end
class LineItem < ActiveRecord::Base
set_primary_keys :node_id, :line_item_id
belongs_to :order, :foreign_key => [:order_node_id, :order_id]
end
Note that as in regular associations, the foreign keys are the same on both sides of
the association, as there is only one foreign key that defines the relationship (even
though, in this case, the foreign key is composed of two attributes).
Pages:
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180