andrzej541
andrzej541

Reputation: 961

Rails: Active Record prevents me from using my model in controller:

I've just start to creating new app from scratch and call a Pocket model from this controller:

class HomeController < ApplicationController  
  def index
  end

  def wallets_index
  end

  def wallets_show
  end

  def wallets_new
    @pocket = Pocket.new
  end
end

and this odd error appeared:

transaction is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.

my schema:

  create_table "pockets", force: :cascade do |t|
    t.string "address"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "transaction"
    t.index ["user_id"], name: "index_pockets_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "pockets", "users"

And Pocket model:

class Pocket < ApplicationRecord
  belongs_to :user
end

This is actually the first time that happened to me and I have no idea which attribute should I rename in order to avoid conflicts?

Upvotes: 0

Views: 55

Answers (2)

Fernand
Fernand

Reputation: 1333

In your pockets table or migration,
transaction is a reserved word, so you cannot use that word.

Upvotes: 1

Gautam
Gautam

Reputation: 1812

You are using the transaction column in your Pocket model

t.integer "transaction"

I would suggest against using a column which has a similar name to a rails method. The transaction method is defined in transcations.rb module of Rails

But, if you really want to use it, you can check safe_attributes gem.

Upvotes: 2

Related Questions