Why ApplicationRecord

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why ApplicationRecord

fugee ohu
Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models  inherit from ApplicationRecord instead of ActiveRecord::Base ?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/25c8ec85-1ec8-4740-8d27-a55a1461b038%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why ApplicationRecord

Aboobacker M K
You can find rationale from original PR which introduced this feature https://github.com/rails/rails/pull/22567


On Fri, Mar 9, 2018 at 8:28 PM, fugee ohu <[hidden email]> wrote:
Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models  inherit from ApplicationRecord instead of ActiveRecord::Base ?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/25c8ec85-1ec8-4740-8d27-a55a1461b038%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards


Aboobacker MK
 
Software Developer | Red Panthers | Engineering
 
phone: <a style="color:#000001" href="tel:8289976088" target="_blank">8289976088

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAP3AYfa%3D-eYFLwNC%2Bk%3Db_ZtqpSTeybcpZAuZKah5wWWBKJ%3Dmkw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why ApplicationRecord

Robert Phillips
In reply to this post by fugee ohu
On Friday, 9 March 2018 14:58:23 UTC, fugee ohu wrote:
>Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models  inherit from ApplicationRecord instead of ActiveRecord::Base ?

I know a guy that doesn't use rails generate controller or rails generate model..  So when he makes a controller he just handcodes it.

For him I suppose it'd be an advantage, as 

It is easier to write 

class Abc < ApplicationController
end

than to write

class Abc < ActionController::Base
  protect_from_forgery with: :exception
end


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8f275c5-fde0-4819-9c62-8d16d176ef77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why ApplicationRecord

Robert Phillips


On Sunday, 11 March 2018 02:39:43 UTC, Robert Phillips wrote:
On Friday, 9 March 2018 14:58:23 UTC, fugee ohu wrote:
>Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models  inherit from ApplicationRecord instead of ActiveRecord::Base ?

I know a guy that doesn't use rails generate controller or rails generate model..  So when he makes a controller he just handcodes it.

For him I suppose it'd be an advantage, as 

It is easier to write 

class Abc < ApplicationController
end

than to write

class Abc < ActionController::Base
  protect_from_forgery with: :exception
end


class Abc should say class AbcController 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9eba08e1-9847-43f5-8a16-1679e04ab639%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why ApplicationRecord

Walter Lee Davis
In reply to this post by Robert Phillips

> On Mar 10, 2018, at 9:39 PM, Robert Phillips <[hidden email]> wrote:
>
> On Friday, 9 March 2018 14:58:23 UTC, fugee ohu wrote:
> >Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models  inherit from ApplicationRecord instead of ActiveRecord::Base ?
>
> I know a guy that doesn't use rails generate controller or rails generate model..  So when he makes a controller he just handcodes it.
>
> For him I suppose it'd be an advantage, as
>
> It is easier to write
>
> class Abc < ApplicationController
> end
>
> than to write
>
> class Abc < ActionController::Base
>   protect_from_forgery with: :exception
> end
>
>

It's meant to be a way to gather common features into one place. If all your routes are meant to have CanCanCan protecting them from unauthorized access, then you may see this in the ApplicationController:

  unless: :devise_controller?
    before_action :authenticate_user!
    load_and_authorize_resource
  end

Which means that you won't forget to add that to your controllers and leave something important exposed to the public. There's lots of other useful stuff you can wedge in the ApplicationController that you need in all controllers, and thus only have to write once. Inheritance can be useful!

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8f275c5-fde0-4819-9c62-8d16d176ef77%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/BA046463-8928-491B-89AF-497E9FB6790C%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.