DB rows: Is create same as

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!

#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.

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.