Rails scope is not null




















As the data required becomes more complex, you can quickly end up with a very convoluted controller action and a model laced with special methods.

In this tutorial, I hope to demonstrate how to chain scopes with the Ruby send method to build a smaller controller. If you are wondering what scopes are, or even pondering what the heck the Ruby send method is, then never fear. They are quite simple. The Ruby send method is a way to send messages to a Ruby object.

You can think of send as just another way to call a method on an object:. I am going to touch on this again so you get a better idea. With our very basic scaffold, we just need to populate it with data. For this, I highly recommend the Faker gem.

In your Gemfile , add the following:. With some data in place, we can now start utilizing scopes to retrieve relevant data from our application.

Before we build the user-facing part of the reporting application, I always like to make sure that things works from the Rails console. Go back to the terminal, fire up a Rails console, and test out one of the new scopes:. Note: The status enum adds a scope for each value of the enum, which is where we get Article. Hopefully, you are beginning to see the power of scopes. Since scopes are exactly like methods, we can take advantage of the incredibly powerful Ruby send method which allows us to pass in a method name as a string for an argument:.

However, chaining methods together using the send method is a little more difficult. Improve this question. Shpigford Shpigford Add a comment. Active Oldest Votes. Have you tried that? Improve this answer. MrYoshiji MrYoshiji No go. I need is null AND empty. You can't compare that to a string.

Then, if they re-activate their account, it clears the timestamp making it empty but not NULL. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back



0コメント

  • 1000 / 1000