Question: I'm taking a course on Lynda.com (Ruby on Rails 5 Essential Training) and I'm having an issue with adding a record on a table. Here's some details: The objective is to create a joint table, Many-to-Many association, so we're first trying to create a record on with of the tables we want to use on the new table. And everytime I write this line: 

```
section = Sections.create(:name => "Section One", :position => 1)

```

It gives me this

```
    (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
 => #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil> 

```

I checked my code and everything seems fine. By the way inserting records on other tables works. It's just this table. 

one important point, This table is a previously created table. It's the new one we're trying to create. 

What am I doing wrong? 

Here is my code from migrate: 

```
class CreateSections < ActiveRecord::Migration[5.2]

  def up
    create_table :sections do |t|

      t.integer   "page_id"
      t.string    "name"
      t.integer   "position"
      t.boolean   "visible", :default => false
      t.string    "content_type"
      t.text      "content"
      t.timestamps
    end
    add_index("sections", "page_id")
  end

  def down
    drop_table :sections
  end

end

```

Here is the Section model:

```
class Section < ApplicationRecord

belongs_to :page
has_many :section_edits

end

```

Answer: 
As Roland suggests, you could use `dcast()`:

```
library(data.table)
dcast(df, col_1 ~ col_2)
##   col_1   B   C   D
## 1     A 2.1 1.3 1.4
## 2     B  NA 4.6  NA

```

Where:

```
df <- data.frame(
  col_1 = c("A", "A", "B", "A"), 
  col_2 = c("B","C", "C", "D"), 
  value = c(2.1, 1.3, 4.6, 1.4)
)

```
With `xtabs` and `mutate_at`. `sparse = TRUE` turns the output to a sparseMatrix:

```
library(dplyr)

mat <- df %>%
  mutate_at(1:2, factor, levels = unique(c(levels(.$col_1), levels(.$col_2)))) %>%
  xtabs(value ~ col_1 + col_2, data=., sparse = TRUE)

mat[lower.tri(mat)] <- mat[upper.tri(mat)]

```

**Result:**

```
4 x 4 sparse Matrix of class "dgCMatrix"
     col_2
col_1   A   B   C   D
    A .   2.1 1.3 1.4
    B 2.1 .   4.6 .  
    C 1.3 1.4 .   .  
    D 4.6 .   .   .  

```