“Belongs To” and “Has One” – aren’t they the same thing?

Rails associations are a great way of mapping relational databases to objects. They add an easy-to-use abstraction over the top of relational concepts like many-to-many relations and spare you some of the details of building joins. But every now and then the abstraction can leave you confused. Nowhere more than in the difference between the… Continue reading

Confused about “has_and_belongs_to_many”? Here’s what it looks like in the database…

The “has_and_belongs_to_many” association and its close friend the “has_many through” association is one of the things that consistently puzzles and confuses newcomers to Rails. And there’s a simple reason for this … whilst in Rails it is a single association, in the database it represents two database relations and a secret, hidden database table. Couple… Continue reading

Confused about “has_many through” associations? Here’s what they look like in the database…

The “has_many through” association and its close friend the “has_and_belongs_to_many” association is one of the things that consistently puzzles and confuses newcomers to Rails. And there’s a simple reason for this … whilst in Rails it is a single association, in the database it represents two database relations. Actually, to be more accurate, the “has_many… Continue reading

What does a “has many” association look like in the database?

I’ve got one of these things and it’s got many of those things. Rails is expressive like that. You generate your model, fill out the migration, push a few views together and there you have it – an application. But what does that “has_many association” actually look like in the database? It’s pretty simple. As… Continue reading

What exactly is going on with the data in my Rails models?

What exactly is going on with the data in my Rails models? For many people, starting out with Ruby on Rails, it’s not just a learning curve with this new environment, with its opinionated ways of doing things. It’s also their first time using a relational database and it’s often hard to find online tutorials… Continue reading

What is a join model? Or has_and_belongs_to_many versus has_many through

Figuring out the associations between your models can be one of the hardest things to wrap your head around. Rails’ ActiveRecord library presents your database as a series of objects; the relations of a relation database converted into associations. Which, on the surface, makes dealing with databases a piece of cake. But in reality it… Continue reading

Joins, associations and relations: the database underneath your ActiveRecord models

Have you ever had issues wrapping your head around associations between your models? Do you need a join model? What is a join model? What is a join? How do you set this stuff up? Relational databases are structured into tables. Each one is basically a spreadsheet – a fixed number of columns and a… Continue reading

What’s the difference between order and sort?

ActiveRecord does some amazing stuff (sometimes). It takes a relational database and then maps it to ruby objects – a task that’s not trivial, as object-orientated design doesn’t really map well to good database design. But sometimes you need to be aware of what ActiveRecord is doing and where it’s doing it … For example,… Continue reading

2 things every ActiveRecord model needs to know

If you’re an ActiveRecord model, life’s pretty easy. Find yourself here, save yourself there, validate yourself every now and then. But there are two questions that every ActiveRecord model needs to know the answer to, otherwise they find that they’re just that bit awkward when dealing with the outside world. The first is pretty easy… Continue reading

The problem with ActiveRecord

The time before ActiveRecord I may be showing my age but I remember the time before Active Record. You would either write raw SQL statements, which for a table with many fields meant a lot of tedious typing, plus the ever-present danger of SQL injection bugs. Or you could use an early Object-Relational Mapper, which… Continue reading