ruby - How to change rails model object variable names that holds object of another model -


say have model , associated schema defined.

class memory < activerecord::base     belongs_to :memory_slot end  class memoryslot < activerecord::base     has_many :memories end 

now typically let me can access memory slots of memory via @memory.memory_slot.name. want access via different method @memory.supporting_memory_slot.name. best way can that?

you won't need new migration, can use previous memory_slot_id, , still can change name following:

class memory < activerecord::base   belongs_to :supporting_memory_slot, class_name: 'memoryslot', foreign_key: 'memory_slot_id' end  class memoryslot < activerecord::base   has_many :memories end 

this way, if had records saved previously, work in current scenario well. if generate new migration, old records saved not accessible, because used using foreign_key memory_slot_id.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -