# DB rows: Is create same as

**URL:** <https://www.ruby-forum.com/t/db-rows-is-create-same-as/124233>\
**Category:** Rails\
**Created:** [December 26, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/db-rows-is-create-same-as/124233 "2007-12-26T20:39:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 26, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/db-rows-is-create-same-as/124233/1 "2007-12-26T20:39:14Z")

</div>

Hi guys,

A question about creating rows and some scaffolding issues.  
At the same time I could describe how I’m doing my stuff, just in case  
I’m doing something wrong.

I have two models.

Users:

class User \< ActiveRecord::Base  
has\_many :tabs  
…some stuff for authentication here  
end

Tabs:

class Tab \< ActiveRecord::Base  
belongs\_to :users  
end

* * *

I create a user and then after authentication it gets access to the form  
from where to create tabs. In that form the user enters some parameters,  
but not all. So in the create method in the controller I get some of  
them from the from and the rest are filled by the controller itself.  
That’s why I use create instead of new for the DB row creation.

def create  
@user = User.find(session[:uid])

```
picture_location = "C:/tabs/aname.png"
midi_location = "C:/midis/aname.mid"

if @user.tabs.create( :user_id => session[:uid],
                      :name => params[:tab][:name],
                      :album => params[:tab][:album],
                      :midi_file_location => midi_location,
                      :tab_picture_location => picture_location )

    #save tab picture
    File.open("C:/tabs/aname.png", "wb") do |f|
    f.write(@params['tab_picture'].read)
    end
    #save tab midi
    File.open("C:/midis/aname.mid", "wb") do |f|
    f.write(@params['tab_audio'].read)
    end
  redirect_to :action => 'list'
end

```

end

QUESTION 1:

The idea is to put the binary objects into the file system if  
@user.tabs.create goes ok. But is a save needed after using create? It  
works fine but I’m am not sure I’m using it correctly though.

QUESTION 2:

def new  
#@user = User.find(session[:uid])  
#@tab = @user.tabs.new  
@tab = Tab.new  
end

The method “new” in the controller contains @tab = Tab.new and that  
works fine. But if I use:

@user = User.find(session[:uid])  
@tab = @user.tabs.new

Also works fine. Could someone explain what is the difference between  
those (if any)? And…why is it needed if I use the create call in the  
real creation of the row?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 27, 2007, 12:15am UTC](https://www.ruby-forum.com/t/db-rows-is-create-same-as/124233/2 "2007-12-27T00:15:01Z")

</div>

#1: You’re creating an object and not modifying it afterwards in the  
code  
you’ve provided, therefore there is no need to call save on that  
object.r

#2: @tab = Tab.new creates a new Tab object with all the attributes set  
to  
nil.  
@tab = @user.tabs.new creates a new Tab object with all the  
attributes  
set to nil except for user\_id, which is set to whatever the id for @user  
is.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 27, 2007, 9:16am UTC](https://www.ruby-forum.com/t/db-rows-is-create-same-as/124233/3 "2007-12-27T09:16:51Z")

</div>

Ryan B. wrote:

> #1: You’re creating an object and not modifying it afterwards in the  
> code  
> you’ve provided, therefore there is no need to call save on that  
> object.r
> 
> #2: @tab = Tab.new creates a new Tab object with all the attributes set  
> to  
> nil.  
> @tab = @user.tabs.new creates a new Tab object with all the  
> attributes  
> set to nil except for user\_id, which is set to whatever the id for @user  
> is.

Thanks so much for the explanation Ryan!

Best regards.
