Ruby Posts

Public, Private and Protected in Ruby

Public: Public methods have maximum visibility

Protected: Protected method can be called with an implicit receiver, as like private. In addition protected method can also be called by an explicit receiver (only) if the receiver is “self” or “an object of the same class”.

Private: For a private method in Ruby, it can never be called with an explicit receiver. We can (only) call the private method with an implicit receiver. This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.

Advertisements

MySql with MongoDB in Rails

This is an area I’ve studied a lot lately, because I have an interest in using NoSQL instead of SQL databases for some of my models. Since we’re on Rails 5 and Mongoid 6 now, I just thought I’d share these steps for setting it up now:

1) Create your Rails app with the ‘–skip-active-record’ switch.

2) Remove sqlite3 from your Gemfile, add Mongoid to your Gemfile, and run ‘bundle’.

3) Run ‘rails g mongoid:config’.

4) Check your ‘application.rb’ file and make sure that inside the ‘class Application’ there is the line ” Mongoid.load! ‘./config/mongoid.yml’ “. It’s sometimes not included when the config is generated, but it’s needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for ‘model’, ‘scaffold’ etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create classes that include the Mongoid::Document module instead of inheriting from ApplicationRecord in the models folder.

—–

And something I’ve also found useful is using both ActiveRecord and Mongoid. MongoDB works great for some types of data, but sometimes I still feel more comfortable with SQL for some data, especially if I feel the need to wrap it in transactions.

1) Create your Rails app.

2) Add Mongoid to your Gemfile and run ‘bundle’.

3) Run ‘rails g mongoid:config’.

4) Check your ‘application.rb’ file and make sure that inside the ‘class Application’ there is the line ” Mongoid.load! ‘./config/mongoid.yml’ “. It’s sometimes not included when the config is generated, but it’s needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for ‘model’, ‘scaffold’ etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create
classes that include the Mongoid::Document module instead of inheriting
from ApplicationRecord in the models folder.

6) The ActiveRecord generators are still available, you just need to specify to use the ‘active_record’ versions as you use them. For example, at this point ‘rails g model user email’ generates a Mongoid model but ‘rails g active_record:model user email’ generates an ActiveRecord model and creates the needed migration. If you chose to make this a Mongoid model, there is no migration to worry about. If you chose to make this an ActiveRecord model, run the migration with ‘rails db:migrate’. (Rails 5 moved rake functionality into the rails command)

7) You can create associations between ActiveRecord and Mongoid models by manually coding methods. You’ll need to consider foreign keys. For example, if an ActiveRecord User has many Mongoid Posts, you’d add a foreign key field to the Post model. Its type would be Integer since ActiveRecord models use the Integer type for their id. Keep in mind that queries made from these associations can be slower because they traverse two data stores. Take things like lazy loading and the n+1 problem into consideration.

Reference: https://gorails.com/guides/setting-up-rails-4-with-mongodb-and-mongoid

Here’s how you can get more work done in less time

When you’re at the office, often times it can be a race against the clock. Meeting deadlines in time is what we all dread while also having to ensure that our work is top notch. But believe it or not, you can actually get more out of the 7-8 hours that you spend at work.

We see people like Elon Musk, Oprah Winfrey, Ariana Huffington working on multiple projects side by side and we wonder how they are able to achieve it. They may have personal assistants who keep track of all their tasks but, believe me when I say, that you can do it too. And you don’t need to hire a personal assistant. All you need is the right frame of mind to become more proactive and get more work done as the clock ticks away.

Here are my top picks for ways you can do more work in less time but I’ll suggest you follow all of them and not just one.

1. List down stuff before you forget it
With the fast-paced nature of work at offices, it is possible someone will give you a task and you’ll forget it unless you write it down. Keep a notebook with you or just take notes on your mobile, whichever suits you. At the start of the day, write down your tasks and check them every time you’ve finished one. Making lists and checking the boxes will drive you to get more work done.

2. Set personal goals
Not setting goals leads to a very aimless life. Same goes for life at work. You should set a target at the beginning of the day or the month about things you want to achieve. I’m not just talking about long term goals like where you want to see yourself in the next five years, I’m also stressing on goals like what you need to achieve by 5 PM. Write them on a sticky note and stick them on your desk. Seeing them in front of you every day will motivate you to strive towards achieving those goals.

3. Learn to prioritize and schedule
Once you’ve made a list of tasks and goals, make another list prioritizing them. If you start working on a task that isn’t due for the next three months while the one that’s due tomorrow is left pending, you’re in trouble. Pick up the high priority tasks first and schedule them throughout the day or the month. Go back to this list time after time to see that you’re on track.

4. Turn off your WiFi
It is a well-known fact that you’ll never get any work done when you are distracted. And the biggest distraction today has to be the constant buzz that comes with being online. Turn off the WiFi on your phone. Someone who needs to reach out to you urgently will call you. Without the distraction of notifications, you’ll be more concentrated on finishing the task at hand.

5. Become a multitasker
You’ll have to be smart if you want to finish your work on time. For that purpose, get to multitasking. Kill two birds with one shot. For example, if you get up from your desk to go for printing also get lunch along the way so that you don’t have to get up for that again. While your video is rendering you can schedule posts for social media. If you’re out to pick up your kids from school you can TCS that important document you had to mail. I can go on but I trust you get the idea.

6. Take short breaks between work
The prioritization and scheduling list I mentioned earlier, follow that strictly. After, let’s say, 40 minutes of full concentrated work take a 5 to 10-minute break in which you can chit chat, use social media, or grab a coffee. Work more and take breaks in short spurts. Taking long breaks shatters your concentration and you’ll waste precious 10 to 15 minutes getting back in the right frame of mind.

 

Source: https://www.techjuice.pk/how-to-get-more-work-done-in-less-time/

“as_json” Rails

as_json has very flexible way to configure complex object according to model relations

EXAMPLE

Model campaign belongs to shop and has one list
Model list has many list_tasks and each of list_tasks has many comments
We can get one json which combines all those data easily.

@campaign.as_json(
    {
        except: [:created_at, :updated_at],
        include: {
            shop: {
                except: [:created_at, :updated_at, :customer_id],
                include: {customer: {except: [:created_at, :updated_at]}}},
            list: {
                except: [:created_at, :updated_at, :observation_id],
                include: {
                    list_tasks: {
                        except: [:created_at, :updated_at],
                        include: {comments: {except: [:created_at, :updated_at]}}
                    }
                }
            },
        },
        methods: :tags
    })

Notice methods: :tags can help you attach any additional object which doesn’t have relations with others. You just need to define a method with name tags in model campaign. This method should return whatever you need (e.g. Tags.all)

Official documentation for as_json

RVM – Gemsets

Creating gemsets

Gemsets must be created before being used. To create a new gemset for the current ruby, do this:
rvm 2.1.1
rvm gemset create teddy
Gemset 'teddy' created.

Alternatively, if you prefer the shorthand syntax offered by rvm use, employ the –create option like so:
rvm use 2.1.1@teddy --create

Select gemsets according to the Project

There is a new way to do this, without having to allow arbitrary shell script to be executed in a .rvmrc file.

Create a file named .ruby-gemset containing only the gemset name in.
multilingo_cms
Need an up to date version of rvm for this to work.

You can also specify the ruby version by creating a file named .ruby-version containing only the ruby version:
1.9.3


This format also has the advantage of being compatible with rbenv and rbfu.

If you have existing projects using the deprecated .rvmrc, you can convert them to the new format using the command:
rvm rvmrc to .ruby-version

RubyMine Ubuntu

rubymine-ide-banner

Install RubyMine

  1. Download RubyMine from Source
  2. Unpack the downloaded archive:
    1. tar -xzf RubyMine-X.Y.Z.tar.gz
  3. Goto extracted folder, nevigate to ‘bin’ directory
    1. ./rubymine.sh
  4. Open any project, goto (Tools) -> (Create Desktop Entry)

Change Rubymine Theme

  1. File -> Settings -> Appearance and Behavior -> Appearance

Remove RubyMine

  1. locate rubymine
  2. locate RubyMine
  3. rm -rf ~/.RubyMine