There is a 'less known' feature of ActiveRecord associations that is very handy: the ability of marking an association as the mirror of another :inverse_of
The best is to rescue an example from the oficial documentation
class Dungeon < ActiveRecord::Base has_many :traps, :inverse_of => :dungeon has_one :evil_wizard, :inverse_of => :dungeon end class Trap < ActiveRecord::Base belongs_to :dungeon, :inverse_of => :traps end class EvilWizard < ActiveRecord::Base belongs_to :dungeon, :inverse_of => :evil_wizard end
dungeon = Dungeon.first
wizard = dungeon.evil_wizard
In a normal configuration, doing wizard.dungeon will hit the database and instantiate a new 'dungeon' (2 instances of the same object).
With :inverse_of , Active record is aware of the relation and will use the same instance in memory. This has 2 benefits:
- we save a DB call
- as both 'dungeons' are the same instance, modifications on dungeon are seen from wizard (an the otehr way around)
Without :inverse_of , a modification on dungeon, is not seen by wizard.dungeon what can lead to subtle errors.
+1 for :inverse_of
Great! thanks for the info!
ReplyDeleteGreate
ReplyDeleteWhat's the point of specifying inverse_of for "has_many <-> belongs_to" association? Aren't they ignored anyway?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIn Rails 4.1 and later they're automatically inferred as long as you don't use class_name or foreign_key. But ... if you do have a more complex belongs_to or has_many then you will need to set inverse_of.
ReplyDeleteHere's a video that has a kinda complex relationship with two foreign keys between a couple models, so it does require explicitly setting the inverse_of. An RSpec test is written to first show that object_ids are different, and then after adding inverse_of they end up to be the same:
https://www.youtube.com/watch?v=5sfufoY59Ek&t=42m54s
In Rails 4.1 and later they're automatically inferred as long as you don't use class_name or foreign_key. But ... if you do have a more complex belongs_to or has_many then you will need to set inverse_of.
ReplyDeleteHere's a video that has a kinda complex relationship with two foreign keys between a couple models, so it does require explicitly setting the inverse_of. An RSpec test is written to first show that object_ids are different, and then after adding inverse_of they end up to be the same:
https://www.youtube.com/watch?v=5sfufoY59Ek&t=42m54s
In Rails 4.1 they're automatically inferred as long as you don't use class_name or foreign_key. But ... if you do have a more complex belongs_to or has_many then you will need to set inverse_of.
ReplyDeleteHere's a video that has a kinda complex relationship with two foreign keys between a couple models, so it does require explicitly setting the inverse_of. An RSpec test is written to first show that object_ids are different, and then after adding inverse_of they end up to be the same:
https://www.youtube.com/watch?v=5sfufoY59Ek&feature=youtu.be&t=42m54s
We provide marketing services to startups and small businesses looking for a partner for their digital media.digital technology institute
ReplyDeleteThank you for sharing very useful information and Local SEO Services Delhi or best Training in India 2021 Amazon SEO Services Delhi
ReplyDeleteVery Informative blog and learn Digital Marketing Course Delhi with 100% Palcement and More courses are
ReplyDeletePPC Course Delhi
SMO Course Delhi
SEO Course Delhi
This Blog is really Helpful
ReplyDeleteClick Here
Click Here
Click Here
Click Here
Click Here
Click Here
Click Here
Click Here
Free Freelance Services
ReplyDeleteFree Freelance Services
Free Freelance Services
Free Freelance Services
Free Freelance Services
Free Freelance Services
Free Freelance Services
Free Freelance Services
Free Freelance Services
We offer the best content writing, SEO, Graphic and website development & web designing services at reasonable prices. Get the best deals on Services of all digital marketing online by Webgross
ReplyDeleteThis blog post is very informative which tells us about The power of 'inverse_of'. Thanks for sharing with us I follow your daily blog post.
ReplyDeleteSocial media marketing services Delhi
Great blogs with very useful information and keep share more information. https://thewebgross.com/content-writing-services-delhi-india/
ReplyDelete