The RoR community is also organizing more and more meetups, conferences and initiatives designed to improve skills and exchange experiences. One such event was Pilot Bootcamp (Community Edition), held between the 22nd of Feb and the 4th of March in Wrocław, Poland.
What was Pilot Bootcamp about?
As you can read on the Pilot Bootcamp event page - it was a two-week free Ruby on Rails crash course where all accepted participants had a chance to code shoulder-to-shoulder with mentors. Five of those mentors were our Netguru developers: Bartek, Grzegorz, Magda G., Magda W. and Dominik. Anna, one of our QA team members, also joined Bootcamp as a participant.
The role of our team was to supervise and teach participants. Each day there was a new set of topics and tasks that participants had to deal with. If anyone needed guidelines or help with resolving issues, our developers were on hand to give hints. The aim was to learn more about Ruby and Ruby on Rails through practice.
Below you can find the most frequently asked questions and our answers to them.
What is the main difference between Gemfile and Gemfile.lock?
Grzegorz: To start, I’ll give you some idea of what a bundler is. It’s a gem manager which allows you to specify the dependencies of your application and helps to install (update, uninstall) them. This bundler creates two files: Gemfile and Gemfile.lock (after installation). Gemfile is like a list of gem dependencies. It also contains a default source (there might be a few), where the bundler will look for all the specified packages. Moreover, you can declare the source of a particular gem, its version, git repository and even its branch. For more details see here.
Okay, you’ve got all the gems specified, what now? It’s time to run bundle install and take a look at Gemfile.lock. This is like a snapshot with the exact versions of the gems and their dependencies that you used last time. You should never edit this file directly because it makes the application consistent with your own third-party code.
If you’d like to know how to handle it, or why bundler exists, you should visit the following link: http://bundler.io/rationale.html
Should I use .build or .build_association?
Grzegorz: If you have associations between models, you should keep this in mind when initializing new objects in your controller. There are two cases to consider: when you have set
has_one associations and
has_and_belongs_to_many associations. For the first case there’s the
.build_association method, where instead of association it should be replaced with the model name. For example:
So in the controller should be:
For the second case, there’s the .build method which returns one or more new objects of the associated type (not yet saved). If you have:
Then you should do it in the following way:
More information about associations are described in the Rails Guides.
What is the MVC pattern used in Rails?
Bartosz: The MVC pattern is an architectural pattern created by Trygve Reenskaug in the 1970s. It’s one of the ways in which a developer can separate classes in object-oriented programming and this pattern is heavily used by Rails. It breaks classes into 3 main layers:
- Models - place where we store all the business logic
- Controllers - the place where we prepare data and execute the business logic stored in models
- View - this is the presentation layer (in web apps it’s mostly HTML documents or JSON objects)
The MVC pattern has evolved a lot since it was proposed and you can read more about it here.
How does the map function work for arrays?
Array#map is one of those methods inspired by a functional paradigm, so we will use a definition of a mathematical function. In a nutshell, for some input (the array on which we call #map) you perform an operation (function) and then receive results (the function’s result). An example of a map is taking an array of integers and finding their squares like so:
Maps function is really useful for transferring from one state to another in an immutable way. This allows you - the developer - to easily write code in a more functional way. Another thing is that you don’t change the state of the array which you pass as an argument to the map. This is a really good way to avoid any unwanted surprises.
Why shouldn’t I validate a foreign key like user_id to check association presence?
Magda: In case you don't have a set foreign key in your database (but actually you should have) you can validate the presence of a foreign key, but it won’t check whether the association really exists. What you really want is to validate the association, not just some number in a database. In the database, in the foreign key column, there can be actually any number (or string) regardless of its correctness – there’s a huge chance that the model with this key doesn’t exist. What is more, in Rails 5 validation for presence of
belongs_to association is triggered by default - so you don't have to worry which attribute you need to validate.
Assuming you have a User model that has many Posts (and the table posts has a
user_id column), you can validate an association like this:
I want to alias my association – what should I do?
Magda: If you want to, for example, call an owner instead of a user and you have a person_id as a foreign key in your database, you will need to override the Rails convention. But this only requires changing one line in your code - Rails will do the rest.
In your model, you want to specify the
foreign_key attributes next to your aliased belongs_to relationship. So in this case it would be as follows:
If you leave only
belongs_to: owner, Rails will be searching for
owner_id in the posts table by default. If you provide only class_name:
User, Rails will be able to map to the appropriate model but… it will be expecting the
owner_id column. If you specify only
foreign_key: person_id, Rails will be expecting to see the
Owner model somewhere.
If you found this article useful, feel free to share it with others! Special thanks to Judyta Draganek for this beautiful picture of bootcampers :)
There's also plenty of other insightful content we share in Ruby Brief, our monthly newsletter about all things Ruby on Rails and web development in general.