DEV Community

Cover image for Using attr_accessor to clean up your calls between models
marelons1337
marelons1337

Posted on • Originally published at blog.rafaljaroszewicz.com

2 1

Using attr_accessor to clean up your calls between models

In my glorious real estate management app I thought that it'd be cool to know how much rent a tenant has to pay without typing tenant.flat.rent every single time (tenant belongs_to :flat)
So instead, we can just use attr_accessor :rent as below.

attr_accessor :rent
def rent
@rent ||= self.flat.rent
end
view raw tenant.rb hosted with ❤ by GitHub

Now I can always access tenant's rent by simply going for tenant.rent

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay