8

obviously I'm quite new to rails, so stick with me.

I've added a constructor to my model

class Movie < Media
  attr_accessible :director, :studio
  attr_accessor :director, :studio

  validates_presence_of :director, :studio, :title
  def initialize title, director, studio
    @title = title
    @director = director
    @studio = studio
  end
end

and that sort of messed up things for me. Befor I've had a method 'new' in my controller like this

def new
    @movies = Movie.new
end

and it worked nicely before the initialize came along. It requires parameters to be passed to the 'new' method, but that was done after a view was open for passing arguments from a user and saving them. Now I can't open that view because I get an error

wrong number of arguments (0 for 3)

The constructor was added due to the fact I started writting test for my application and setting default vaules for the constructor will nullify that test. Suggestion on solving this?

EDIT: My tests look like this:

require 'spec_helper'

describe Movie do

    before :each do 
        @movie = Movie.new "Bullet", "John", "20th"
    end
    describe "#{new}" do
        it "returns new object of Movie" do
            @movie.should be_an_instance_of Movie
        end
        it "throws ArgumentError when give less than 3 parameters" do
            lambda {Movie.new(:director => "John", :studio => "20th")}.should raise_exception ArgumentError
        end
    end

    describe "#title" do
        it "returns the correct title" do
            @movie.title.should eql "Bullet"
        end
    end
    describe "#director" do
        it "returns the correct director" do
            @movie.director.should eql "John"
        end
    end
    describe "#studio" do
        it "returns the correct studio" do
            @movie.studio.should eql "20th"
        end
    end
end

Without that constructor all the tests fail....

radical_edo
  • 934
  • 2
  • 11
  • 29
  • Suggestion: If you just need to set defaults for testing, use a factory for the testing which provides ways to set defaults and don't bleed this into your application code. If you need to do this for other reasons, [see this question](http://stackoverflow.com/questions/328525/what-is-the-best-way-to-set-default-values-in-activerecord) – numbers1311407 Feb 09 '13 at 16:59
  • I do provied arguments in my tests. Not the problem. Test was checking if there are less than 3 arguments provieded, to do that had to add the constructor, by doing so this problem occured. – radical_edo Feb 10 '13 at 10:47
  • Ok, new suggestion: Don't write tests that break core active_record functionality for no reason, forcing you to write code counter to widely used rails components to fix them. `ActiveRecord::Base#initialize` accepts a parameter object, not a list of arguments. If you insist on breaking such an primary piece of active_record then this is going to be the first of many problems for you. – numbers1311407 Feb 10 '13 at 18:29

1 Answers1

13

The default constructor provided by ActiveModel is pretty good. If you delete the constructor you wrote, you should be able to use the default constructor like this:

@movie = Movie.new(title: 'The Hobbit', director: 'Peter Jackson', studio: 'New Line Cinema')

When you don't want to provide the three arguments (like in your new action), you can stick with @movie = Movie.new

graysonwright
  • 514
  • 2
  • 8
  • 1
    Also, a really good resource to get better with this kind of thing is [Rails for Zombies](http://railsforzombies.org/) – graysonwright Jan 15 '14 at 22:28