alaup
alaup

Reputation: 61

Rails Parent.find method returns object of type Child

I'm seeing an interesting behavior and would like some insight.

I have two models: Person & User. User inherits from Person. There is no users db table, just people.

When I search for a user I've created using Person's find model, I'm expecting to see an object of type person returned. But its type is user.

@person = Person.find(params[:id])
@person.class 
# outputs 'User'

Can someone please explain why it wouldn't return a person object.

Upvotes: 1

Views: 267

Answers (1)

Daniel Rikowski
Daniel Rikowski

Reputation: 72514

That is the desired behaviour. The type column of the found person row contains the value "User". Because of that the returned type is User, too.

Remember that User also is a Person, so you can treat any User instance like it was a Person instance.

Upvotes: 3

Related Questions