fix foreign key should be ObjectID

This commit is contained in:
Wen-Tien Chang 2010-02-05 16:53:52 +08:00
parent 8f5c6d1a56
commit 11991320d2
3 changed files with 29 additions and 12 deletions

View File

@ -4,8 +4,19 @@ class Component < Item
key :engine_name, String key :engine_name, String
key :layout_name, String, :required => true key :layout_name, String, :required => true
key :layout_id, String, :required => true key :layout_id, ObjectId, :required => true
belongs_to :layout belongs_to :layout
protected
def setup_default_value
super
if self.layout_name
self.layout_id = Layout.find_by_name( self.layout_name ).id
end
end
end end

View File

@ -7,7 +7,7 @@ class Item
key :name, String, :required => true, :index => true key :name, String, :required => true, :index => true
key :parent_name, String, :index => true key :parent_name, String, :index => true
key :parent_id, String, :index => true key :parent_id, ObjectId, :index => true
key_i18n :title, String, :required => true key_i18n :title, String, :required => true
@ -16,10 +16,10 @@ class Item
validates_format_of :name, :with => /^[a-zA-Z-_]+$/ validates_format_of :name, :with => /^[a-zA-Z-_]+$/
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
many :children, :class_name => "Item", :foreign_key => :parent_id many :children, :class_name => "Item", :foreign_key => :parent_id, :dependent => :destroy
before_validation :setup_default_value before_validation :setup_default_value
def self.find_by_name(item_name) def self.find_by_name(item_name)
Item.find(:first, :conditions => { :name => item_name, :is_published => true }) Item.find(:first, :conditions => { :name => item_name, :is_published => true })
end end
@ -31,7 +31,7 @@ class Item
end end
protected protected
def setup_default_value def setup_default_value
if self.position.blank? if self.position.blank?
max_page = Page.find(:last, :order => 'position') max_page = Page.find(:last, :order => 'position')
@ -43,12 +43,7 @@ class Item
self.parent_id = nil self.parent_id = nil
else else
self.parent_id = Item.find_by_name( self.parent_name ).id self.parent_id = Item.find_by_name( self.parent_name ).id
end end
if self.layout_name
self.layout_id = Layout.find_by_name( self.layout_name ).id
end
end end
end end

View File

@ -4,8 +4,19 @@ class Page < Item
key_i18n :content, String key_i18n :content, String
key :layout_name, String, :required => true key :layout_name, String, :required => true
key :layout_id, String, :required => true key :layout_id, ObjectId, :required => true
belongs_to :layout belongs_to :layout
protected
def setup_default_value
super
if self.layout_name
self.layout_id = Layout.find_by_name( self.layout_name ).id
end
end
end end