1

I have a table, created from a database migration:

class CreateTickets < ActiveRecord::Migration
def up
create_table :tickets, :primary_key => :tickets_id do |t|
  t.string :title, null: false
  t.text :body
  t.datetime :create_date
  t.integer :author_id
  t.integer :status, null: false, default: 0
end
end

A model:

class Ticket < ActiveRecord::Base
attr_accessible :title, :body, :create_date, :author_id, :status
end

And when I try to create a record:

User.create(title: title,body: body,create_date: Time.zone.now, author_id: @author_id,status: 0)

I get this error:

Can't mass-assign protected attributes: title, body, create_date, author_id, status

What did I do wrong?

dio_bless
  • 435
  • 2
  • 5
  • 13

3 Answers3

1

You're trying to create User instead of creating Ticket.

Change your code to:

Ticket.create(title: title, body: body, create_date: Time.zone.now, author_id @author_id, status: 0)

Hope, that it'll help.

Peter Tretyakov
  • 3,380
  • 6
  • 38
  • 54
1

You are trying to create a User instance and not a ticket.

Leftis
  • 119
  • 9
1

Also, if you hate marking each and every attribute in the accessible-list, you would prefer adding this in your config/application.rb

config.active_record.whitelist_attributes = false

Satya Kalluri
  • 5,148
  • 4
  • 28
  • 37