shaan
shaan

Reputation: 524

How and where rails define getter and setter methods for model attribute

Hi I'm newbie in Rails,

I'm eager to know how and where rails define a getter/setter methods for columns name

Let us suppose I have a name column in users table

User.find_by_name "some name". How rails resolve it behind the scene.

Upvotes: 0

Views: 201

Answers (1)

widjajayd
widjajayd

Reputation: 6253

your question is about Attribute based accessor methods

for example if you have User Object with 2 properties first_name and last_name, rails map this class to a Model User.

if you open User modellocated in /app/models/User.rb

class User < ApplicationRecord
  # if you see line above meaning this class
  # inherit from ApplicationRecord
  # and there are many methods inside parent class
  # instance level methods and class level methods
  ...
end

@user = User.new
# create new instance
@user.changed?
@user.first_name = 'John'
@user.changed_attributes
# these 2 methods (changed? and changed_attributes) inherit from ApplicationRecord

# class methods
User.find_by_first_name('John')
# beside instance methods rails also inherit class level methods if you use model name User (not instance name @user)

references:

Upvotes: 1

Related Questions