# ActiveRecord::Base.new

**URL:** <https://www.ruby-forum.com/t/activerecord-base-new/136891>\
**Category:** Rails\
**Created:** [May 7, 2008, 6:14am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891 "2008-05-07T06:14:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 6:14am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/1 "2008-05-07T06:14:56Z")

</div>

I am new to RoR (new to both Ruby and Rails) and am creating a test  
‘wizard’ that allows to enter information for an employee through  
several pages. Only when reaching the last one and passing all  
validations through all the pages a row should be created in the  
table; however I’ve found that when I issue ‘Employee.new’ a row is  
automatically created EVERY TIME and whenever I press enter on any of  
the pages the page’s data gets automatically transferred to the newly  
created row with no ‘save’ or ‘create’ operation being executed on my  
part.

This causes trouble for a couple of reasons:

1. It is not the desired behavior.
2. All automatically created rows stay in the table, regardless if I  
save them or not. Some of them empty and others partially filled with  
information.

The database is MySQL in case it makes a difference.

Does anybody know what could be going on? And most importantly how I  
can make it work the way I need it to work?

Thanks a lot to everybody.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 7:18am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/2 "2008-05-07T07:18:46Z")

</div>

Calling new on a model will not enter rows into the table. You are  
somewhere  
calling either save on that object or skipping new and calling create.

Pastie your model please.

On Wed, May 7, 2008 at 1:36 PM, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks a lot to everybody.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/3 "2008-05-07T21:12:51Z")

</div>

This may help you out.

> **[GitHub - adkron/actsaswizard: A Rails plugin to make creating a wizard easy and...](https://github.com/Adkron/actsaswizard/tree/master)**
>
> master

It is a plugin for creating wizards.

Amos

On Tue, May 6, 2008 at 11:06 PM, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks a lot to everybody.
> 
> >

## – Amos K. A. King Software Development and Consulting, L.C. [http://dirtyInformation.com](http://dirtyInformation.com)

Looking for something to do? Visit [http://ImThere.com](http://ImThere.com)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/4 "2008-05-07T22:28:47Z")

</div>

pepe wrote:

> def validate( action\_name )  
> get\_employee  
> if @employee.errors.empty?  
> update\_employee  
> if action\_name == ‘name’  
> #@employee.validate\_name  
> validate\_name  
> elsif action\_name == ‘address’  
> #@employee.validate\_address  
> validate\_address  
> elsif action\_name == ‘phones’  
> #@employee.validate\_phones  
> validate\_phones  
> end  
> redirect\_to :action =\> action\_name.to\_sym if !  
> @employee.errors.empty?  
> end
> 
> def update\_employee  
> @employee.update\_attributes(params[:employee])  
> end

Your issue here is the update\_attributes method in your validations.  
According to the rails docs:  
Updates all the attributes from the passed-in Hash and saves the record.

Andrew

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 10:14pm UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/5 "2008-05-07T22:14:16Z")

</div>

Hi Ryan.

Thanks for taking an interest on this. I am only using ‘save’ on the  
last page’s action method in the controller (4 pages: name, address,  
phones and save; page ‘save’ has a button to invoke ‘new’, which just  
redirects to ‘name’). There is no use of ‘create’ anywhere in the  
model, the controller or the view. I created some validation methods  
in the model because that’s where they are supposed to be but seeing  
what was happening I took them out and put the validations in the  
controller to see if somehow the model was using functionality I  
didn’t know it might have that would post the data to the row whenever  
a method is invoked (just trying to guess). The problem persists.  
Below is the full controller’s code and the model is now empty. The  
table was created with a migration so in addition to the fields in the  
validation methods there is the ‘id’ field in it. Thanks again for  
helping:

class EmployeesController \< ApplicationController  
def name  
get\_employee  
flash[:employee] = @employee  
end

def address  
validate ‘name’  
end

def phones  
validate ‘address’  
end

def save  
validate ‘phones’  
return if !@employee.errors.empty?  
@employee.save  
end

def new  
flash[:employee] = Employee.new  
redirect\_to :action =\> :name  
end

private  
def validate( action\_name )  
get\_employee  
if @employee.errors.empty?  
update\_employee  
if action\_name == ‘name’  
#@employee.validate\_name  
validate\_name  
elsif action\_name == ‘address’  
#@employee.validate\_address  
validate\_address  
elsif action\_name == ‘phones’  
#@employee.validate\_phones  
validate\_phones  
end  
redirect\_to :action =\> action\_name.to\_sym if !  
@employee.errors.empty?  
end

```
flash[:employee] = @employee

```

end # def validate( method\_name )

def get\_employee  
@employee = flash[:employee] ||= Employee.new  
end

def update\_employee  
@employee.update\_attributes(params[:employee])  
end

def validate\_name  
@employee.errors.clear  
@employee.errors.add(:first, ‘Please enter first name.’) unless !  
@employee.first.nil? && @employee.first != ‘’  
@employee.errors.add(:last, ‘Please enter last name.’) unless !  
@employee.last.nil? && @employee.last != ‘’  
@employee.errors.add(:identification, ‘Please enter  
identification.’) unless !@employee.identification.nil? &&  
@employee.identification != ‘’  
end

def validate\_address  
@employee.errors.clear  
@employee.errors.add(:street, ‘Please enter street.’) unless !  
@employee.street.nil? && @employee.street != ‘’  
@employee.errors.add(:city, ‘Please enter city.’) unless !  
@employee.city.nil? && @employee.city != ‘’  
@employee.errors.add(:state, ‘Please enter state.’) unless !  
@employee.state.nil? && @employee.state != ‘’  
@employee.errors.add(:zip, ‘Please enter zip code.’) unless !  
@employee.zip.nil? && @employee.zip != ‘’  
end

def validate\_phones  
@employee.errors.clear  
@employee.errors.add(:phone, ‘Please enter phone number.’) unless !  
@employee.phone.nil? && @employee.phone != ‘’  
@employee.errors.add(:cell, ‘Please enter cell number.’) unless !  
@employee.cell.nil? && @employee.cell != ‘’  
end

end

On May 7, 1:17 am, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 7, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/6 "2008-05-07T22:37:34Z")

</div>

BTW - validations really belong in the model rather than the controller.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 8, 2008, 1:14am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/7 "2008-05-08T01:14:37Z")

</div>

Thanks!

I guess I’ve been too busy trying to learn RoR and at the same time  
make the code work. I didn’t even think that the method would save the  
data and just got it from a sample out of a book as a quick and easy  
way to update all attributes from the form.

I owe you a beer. 🙂

On May 7, 4:28 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 8, 2008, 3:20am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/8 "2008-05-08T03:20:28Z")

</div>

pepe wrote:

> Thanks!
> 
> I guess I’ve been too busy trying to learn RoR and at the same time  
> make the code work. I didn’t even think that the method would save the  
> data and just got it from a sample out of a book as a quick and easy  
> way to update all attributes from the form.
> 
> I owe you a beer. 🙂
> 
> On May 7, 4:28 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

You’re welcome 🙂

If you wish to continue using your controllers to do the validations (I  
strongly suggest you don’t), then you might want to investigate the  
merge and reverse\_merge functions.

Oh - and I drink almost any beer 🙂

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 8, 2008, 3:25am UTC](https://www.ruby-forum.com/t/activerecord-base-new/136891/9 "2008-05-08T03:25:52Z")

</div>

pepe wrote:

> Thanks!
> 
> I guess I’ve been too busy trying to learn RoR and at the same time  
> make the code work. I didn’t even think that the method would save the  
> data and just got it from a sample out of a book as a quick and easy  
> way to update all attributes from the form.
> 
> I owe you a beer. 🙂
> 
> On May 7, 4:28 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I really should think about what I want to say before I start typing. I  
meant to include some pointers on moving validations into the model.

Let’s take your code:  
def validate\_address  
@employee.errors.clear  
@employee.errors.add(:street, ‘Please enter street.’) unless !  
@employee.street.nil? && @employee.street != ‘’  
@employee.errors.add(:city, ‘Please enter city.’) unless !  
@employee.city.nil? && @employee.city != ‘’  
@employee.errors.add(:state, ‘Please enter state.’) unless !  
@employee.state.nil? && @employee.state != ‘’  
@employee.errors.add(:zip, ‘Please enter zip code.’) unless !  
@employee.zip.nil? && @employee.zip != ‘’  
end

If we move this validation into the Employee model it might look like  
this:

class Employee \< ActiveRecord::Base  
validates\_presence\_of :street, :city, state, :zip  
end

See how much cleaner that is? It has the added benefit of enforcing  
these validations for the employee no matter which controller is using  
them (in your current configuration you will be repeating yourself all  
over the place).

Andrew
