Alexander Luna Alexander Luna - 3 years ago 143
Ruby Question

Rails 4.2.1: Cannot create record

today I tried creating a new model like usual I created my controller first:

rails g controller Newsletters


Then I created my model:

rails g model Newsletter


Then I updated my migration:

class CreateNewsletters < ActiveRecord::Migration
def change
create_table :newsletters do |t|
t.string :subject
t.text :message
t.string :recipient
t.boolean :send, default: false

t.timestamps null: false
end
end
end


Finally I ran my migration:

rake db:migrate


I setup my controller:

class NewslettersController < ApplicationController
def index
@newsletters = Newsletter.all
end

def show
@newsletter = Newsletter.find_by(id: params[:id])
end

def new
@newletter = Newsletter.new
end

def create
@newletter = Newsletter.new(newsletter_params)
if @newsletter.save
flash[:success] = "Newsletter saved"
redirect_to newletter_path
else
render 'new'
end
end

def edit
end

def update
end

def destroy
newsletter = Newsletter.find_by(id: params[:id]).destroy
flash[:success] = "Successfully destroyed"
end


private
def newsletter_params
params.require(:newsletter).permit(:subject, :message, :recipient, :send)
end
end


Now here is the problem. When I try to create a record in the console I get this error:

irb(main):001:0> newsletter = Newsletter.create(subject: "test", message: "test test", recipient: "all")
ArgumentError: wrong number of arguments (given 2, expected 0)
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:48:in `__temp__3756e646'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:379:in `clone_attribute_value'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:122:in `old_attribute_value'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:50:in `__temp__375726a6563647'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:33:in `create'
from (irb):1
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/alexander/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'irb(main):002:0>


What I tried this far:


  1. I rolled everything back and removed all files generated and did it all by hand again.

  2. I install the bundle again in case something is outdated



I don't really get what the error is so if someone could help me I would appreciate it a lot.

PS: I have read on github that it could be a problem with the "rb-fsevent" gem but my logs dont show the same thing:

https://github.com/guard/listen/issues/431

UPDATE:

My Newsletter model

class Newsletter < ActiveRecord::Base
end


PS: I am running ruby 2.3.0

Answer Source

send is a Ruby method and it leads to problems when you have an attribute with the same name.

Just rename that column to something else - like email_send or was_send and you will be fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download