Src
Src

Reputation: 5482

Is Rails ORM working this way?

Recently, i needed to implement ORM like Active Record but using php. So the first thing i want to write is the record creation. As long as every record treated like an object, you can assign values directly into the class attributes like so:

p = Product.new
p.name = "Some Book"

And than creation method called.

So, what i want to know, if ActiveRecord first sends query to the database with "DESCRIBE table_name" to look what columns it has and then turn them into class attributes?

If it's like so, is this method good for performance (you need to send query every time you make an object of the model)?

Upvotes: 2

Views: 63

Answers (1)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

No, ActiveRecord doesn't query db when you create new instance of a Model class. In ruby, attr_accessor helps in defining getters and setters, and in rails you can verify in console as

p = Product.new
#=> #<Product id: nil, created_at: nil, updated_at: nil>

No db call, just a new Product class object.

Upvotes: 1

Related Questions