Method: SimpleRecord::Base.belongs_to
- Defined in:
- lib/simple_record.rb
.belongs_to(association_id, options = {}) ⇒ Object
One belongs_to association per call. Call multiple times if there are more than one.
This method will also create an {association)_id method that will return the ID of the foreign object without actually materializing it.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/simple_record.rb', line 361 def self.belongs_to(association_id, = {}) attribute = SimpleRecord::Base::Attribute.new(:belongs_to) defined_attributes[association_id] = attribute attribute. = #@@belongs_to_map[association_id] = options arg = association_id arg_s = arg.to_s arg_id = arg.to_s + '_id' # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us # puts "arg_id=#{arg}_id" # puts "is defined? " + eval("(defined? #{arg}_id)").to_s # puts 'atts=' + @attributes.inspect # Define reader method send(:define_method, arg) do attribute = defined_attributes_local[arg] = attribute. # @@belongs_to_map[arg] class_name = [:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length] # Camelize classnames with underscores (ie my_model.rb --> MyModel) class_name = class_name.camelize # puts "attr=" + @attributes[arg_id].inspect # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil? ret = nil arg_id = arg.to_s + '_id' if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != '' if !@@cache_store.nil? arg_id_val = @attributes[arg_id][0] cache_key = self.class.cache_key(class_name, arg_id_val) # puts 'cache_key=' + cache_key ret = @@cache_store.read(cache_key) # puts 'belongs_to incache=' + ret.inspect end if ret.nil? to_eval = "#{class_name}.find(@attributes['#{arg_id}'][0])" # puts 'to eval=' + to_eval begin ret = eval(to_eval) # (defined? #{arg}_id) rescue RightAws::ActiveSdb::ActiveSdbError if $!..include? "Couldn't find" ret = nil else raise $! end end end end # puts 'ret=' + ret.inspect return ret end # Define writer method send(:define_method, arg.to_s + "=") do |value| arg_id = arg.to_s + '_id' if value.nil? make_dirty(arg_id, nil) self[arg_id]=nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save else make_dirty(arg_id, value.id) self[arg_id]=value.id end end # Define ID reader method for reading the associated objects id without getting the entire object send(:define_method, arg_id) do if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != '' return @attributes[arg_id][0] end return nil end # Define writer method for setting the _id directly without the associated object send(:define_method, arg_id + "=") do |value| if value.nil? self[arg_id] = nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save else self[arg_id] = value end end send(:define_method, "create_"+arg.to_s) do |*params| newsubrecord=eval(arg.to_s.classify).new(*params) newsubrecord.save arg_id = arg.to_s + '_id' self[arg_id]=newsubrecord.id end end |