[Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

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

[Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

Léo Santos
Hi there!

I would like to introduce a feature that can expedite development, which by the way I think it's one of the greatest advantages of using Ruby on Rails. So, looking to follow the Convention Over Configuration standard, lets say you have:

# a users table
create_table "users", force: true do |t|
  t.boolean  "active", default: true
end

# a User model
class User < ActiveRecord::Base
end

The new feature would automatically add the scope to fetch active users like:

   >User.active

without having to explicitly define it on the model. It is similar to a feature that has already been implemented to boolean attributes that allow us to do things like:

      >user = User.new(active: true)
   >user.active? 
     #=> true 

Do you think it's worthy merging it into Rails core?

Best regards,
Leonardo Santos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

Kasper Timm Hansen
I don’t think we’d like to autogenerate scopes for your models based on the column type, no.


You might want to look into using an enum backed by an integer ”status” column instead of the boolean column.

That generates a scope as well as making it really easy to add another state, should you need ever need to do so.

Den 4. jan. 2018 kl. 13.16 skrev Léo Santos <[hidden email]>:

Hi there!

I would like to introduce a feature that can expedite development, which by the way I think it's one of the greatest advantages of using Ruby on Rails. So, looking to follow the Convention Over Configuration standard, lets say you have:

# a users table
create_table "users", force: true do |t|
  t.boolean  "active", default: true
end

# a User model
class User < ActiveRecord::Base
end

The new feature would automatically add the scope to fetch active users like:

   >User.active

without having to explicitly define it on the model. It is similar to a feature that has already been implemented to boolean attributes that allow us to do things like:

      >user = User.new(active: true)
   >user.active? 
     #=> true 

Do you think it's worthy merging it into Rails core?

Best regards,
Leonardo Santos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

radhames brito
Hi Leo.
I think it makes more sense to make this an op-in thing like this

class User < ActiveRecord::Base
 boolean_scope :active
end

And then you have the scope ```User.active```, with the extra option of doing ```boolean_scope :active, negative: :inactive``` that would create the scope for the false case.
This of course would raise if there is not column matching the name, since any more work and is easier to declare the scope. Just my 2 cents.



On Thu, Jan 4, 2018 at 8:53 AM, Kasper Timm Hansen <[hidden email]> wrote:
I don’t think we’d like to autogenerate scopes for your models based on the column type, no.


You might want to look into using an enum backed by an integer ”status” column instead of the boolean column.

That generates a scope as well as making it really easy to add another state, should you need ever need to do so.

Den 4. jan. 2018 kl. 13.16 skrev Léo Santos <[hidden email]>:

Hi there!

I would like to introduce a feature that can expedite development, which by the way I think it's one of the greatest advantages of using Ruby on Rails. So, looking to follow the Convention Over Configuration standard, lets say you have:

# a users table
create_table "users", force: true do |t|
  t.boolean  "active", default: true
end

# a User model
class User < ActiveRecord::Base
end

The new feature would automatically add the scope to fetch active users like:

   >User.active

without having to explicitly define it on the model. It is similar to a feature that has already been implemented to boolean attributes that allow us to do things like:

      >user = User.new(active: true)
   >user.active? 
     #=> true 

Do you think it's worthy merging it into Rails core?

Best regards,
Leonardo Santos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
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: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

Kevin Deisz
IIRC, this was actually a thing in Rails 2 and it got taken out. I agree with you about the convention over configuration idea, but when you only need the one line of code to get it back, and it relieves a maintenance burden and makes things faster, I'd have to side with it not being in core.

That being said, this is one of those things that would be great for a gem to provide automatically (basically on initialize search through for every boolean column and set up scopes). Sounds like a good project if you've got time to take it on.

On Thu, Jan 4, 2018 at 9:49 AM, radhames brito <[hidden email]> wrote:
Hi Leo.
I think it makes more sense to make this an op-in thing like this

class User < ActiveRecord::Base
 boolean_scope :active
end

And then you have the scope ```User.active```, with the extra option of doing ```boolean_scope :active, negative: :inactive``` that would create the scope for the false case.
This of course would raise if there is not column matching the name, since any more work and is easier to declare the scope. Just my 2 cents.



On Thu, Jan 4, 2018 at 8:53 AM, Kasper Timm Hansen <[hidden email]> wrote:
I don’t think we’d like to autogenerate scopes for your models based on the column type, no.


You might want to look into using an enum backed by an integer ”status” column instead of the boolean column.

That generates a scope as well as making it really easy to add another state, should you need ever need to do so.

Den 4. jan. 2018 kl. 13.16 skrev Léo Santos <[hidden email]>:

Hi there!

I would like to introduce a feature that can expedite development, which by the way I think it's one of the greatest advantages of using Ruby on Rails. So, looking to follow the Convention Over Configuration standard, lets say you have:

# a users table
create_table "users", force: true do |t|
  t.boolean  "active", default: true
end

# a User model
class User < ActiveRecord::Base
end

The new feature would automatically add the scope to fetch active users like:

   >User.active

without having to explicitly define it on the model. It is similar to a feature that has already been implemented to boolean attributes that allow us to do things like:

      >user = User.new(active: true)
   >user.active? 
     #=> true 

Do you think it's worthy merging it into Rails core?

Best regards,
Leonardo Santos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
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: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Kevin D. Deisz

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

Geoff Harcourt
Hi Leo,

A suggestion if you wanted to build this feature as a gem: the Pacecar gem used to add these scopes automatically. It's no longer maintained, but you could dig up some of the code to just get the automatic scopes for booleans.


-Geoff

On Thu, Jan 4, 2018 at 10:13 AM, Kevin Deisz <[hidden email]> wrote:
IIRC, this was actually a thing in Rails 2 and it got taken out. I agree with you about the convention over configuration idea, but when you only need the one line of code to get it back, and it relieves a maintenance burden and makes things faster, I'd have to side with it not being in core.

That being said, this is one of those things that would be great for a gem to provide automatically (basically on initialize search through for every boolean column and set up scopes). Sounds like a good project if you've got time to take it on.

On Thu, Jan 4, 2018 at 9:49 AM, radhames brito <[hidden email]> wrote:
Hi Leo.
I think it makes more sense to make this an op-in thing like this

class User < ActiveRecord::Base
 boolean_scope :active
end

And then you have the scope ```User.active```, with the extra option of doing ```boolean_scope :active, negative: :inactive``` that would create the scope for the false case.
This of course would raise if there is not column matching the name, since any more work and is easier to declare the scope. Just my 2 cents.



On Thu, Jan 4, 2018 at 8:53 AM, Kasper Timm Hansen <[hidden email]> wrote:
I don’t think we’d like to autogenerate scopes for your models based on the column type, no.


You might want to look into using an enum backed by an integer ”status” column instead of the boolean column.

That generates a scope as well as making it really easy to add another state, should you need ever need to do so.

Den 4. jan. 2018 kl. 13.16 skrev Léo Santos <[hidden email]>:

Hi there!

I would like to introduce a feature that can expedite development, which by the way I think it's one of the greatest advantages of using Ruby on Rails. So, looking to follow the Convention Over Configuration standard, lets say you have:

# a users table
create_table "users", force: true do |t|
  t.boolean  "active", default: true
end

# a User model
class User < ActiveRecord::Base
end

The new feature would automatically add the scope to fetch active users like:

   >User.active

without having to explicitly define it on the model. It is similar to a feature that has already been implemented to boolean attributes that allow us to do things like:

      >user = User.new(active: true)
   >user.active? 
     #=> true 

Do you think it's worthy merging it into Rails core?

Best regards,
Leonardo Santos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
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: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Kevin D. Deisz

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
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: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature request] [ActiveRecord] Automatically create scopes for models boolean attributes

Léo Santos
In reply to this post by Léo Santos
That's great guys! Will definitely try to take a deeper look into it and maybe build this as a gem. Thanks for the feedback

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" 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].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.