Topic: Help with getting a value from another table

This code doesn't work and throws an error. I need the scope.scope to be concatenated with other fields.Is it 'cos the field name and Table name are same?

def create
    @subreq = Subreq.new(params[:subreq])
    @subreq.resource_ids = Resource.find(params[:resource_ids]) if params[:resource_ids]
    @subreq.bfield = Bfield.find(params[:bfield_ids]) if params[:bfield_ids]
    @subreq.itfield = Itfield.find(params[:itfield_ids]) if params[:itfield_ids]
    #These are added in case there are validation errors when saving and the new method is re-rendered
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all
    @user = session[:user]
    @subreq.user_id = @user.id
    @scope = session[:scope]
    @subreq.scope_id = @scope.id
       
       
    if @subreq.sub_function.blank?
       @subreq.real_number = @subreq.br_tr_no + "." + @scope.scope + "." + @subreq.req_extension
    else
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.sub_function + "." + @scope.scope + "." + @subreq.req_extension
    end

Re: Help with getting a value from another table

What error are you getting? The stack trace would help too.

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

I changed the scope.scope field to scope.scope_id

undefined method `scope_id' for "3":String
RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/subreqs_controller.rb:104:in `create'

Re: Help with getting a value from another table

The session is just storing the id, so you need to use that to fetch the scope:

@scope = Scope.find(session[:scope])

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

Ryan, it still gives the error:
NoMethodError in SubreqsController#create
undefined method `scope_id' for "3":String
RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/subreqs_controller.rb:104:in `create'

I changed the statement in method "new"
@scope  = Scope.find(session[:scope_id])

Re: Help with getting a value from another table

deepti.jerath wrote:

I changed the statement in method "new"
@scope  = Scope.find(session[:scope_id])

You need to change it in the create action.

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

Ok, the Deal now is it's giving no more errors but strange that it's storing a junk value in the subreqs table for scope_id as "54805530" instead of value 2.

Re: Help with getting a value from another table

Not sure what the problem is. Try opening script/console and playing around with the models there.

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

Thanks Ryan for all your help. It finally worked with this piece of code as you suggested.

def create
    @subreq = Subreq.new(params[:subreq])
    @subreq.resource_ids = Resource.find(params[:resource_ids]) if params[:resource_ids]
    @subreq.bfield = Bfield.find(params[:bfield_ids]) if params[:bfield_ids]
    @subreq.itfield = Itfield.find(params[:itfield_ids]) if params[:itfield_ids]
    #These are added in case there are validation errors when saving and the new method is re-rendered
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all
    @user = session[:user]
    @subreq.user_id = @user.id
    @scope = Scope.find(session[:scope])
    @subreq.scope_id = @scope.id
       
       
    if @subreq.sub_function.blank?
       @subreq.real_number = @subreq.br_tr_no + "." + @scope.scope_id + "." + @subreq.req_extension
    else
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.sub_function + "." + @scope.scope_id + "." + @subreq.req_extension
    end

Re: Help with getting a value from another table

BTW, you can simplify the setting of real_number to this. Then you won't need that if condition.

@subreq.real_number = [@subreq.br_tr_no, @subreq.sub_function, @scope.scope_id, @subreq.req_extension].reject(&:blank?).join('.')

To further clean this up I recommend moving this into a method in the Subreq model:

# in Subreq
def real_number
  [br_tr_no, sub_function, scope_id, req_extension].reject(&:blank?).join('.')
end

Since it can be generated dynamically there's no sense in storing it in an attribute/database column.

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

It didn't work Ryan. I created a method and it leaves Real_number as blank in the Database and on the screen. I tried to assign it to the array but it gave me an error. I need to store it back in the Database too.

@subreq.real_number = real_number

Last edited by deepti.jerath (2007-03-05 13:11:58)

Re: Help with getting a value from another table

Did you try it inline (not a separate method)?

@subreq.real_number = [@subreq.br_tr_no, @subreq.sub_function, @scope.scope_id, @subreq.req_extension].reject(&:blank?).join('.')

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

It gives error:

NameError in SubreqsController#update
undefined local variable or method `br_tr_no' for #<SubreqsController:0x6723b60>

Re: Help with getting a value from another table

It should be calling that method on the model, not the controller. Are you certain you copied it exactly like this?

@subreq.real_number = [@subreq.br_tr_no, @subreq.sub_function, @scope.scope_id, @subreq.req_extension].reject(&:blank?).join('.')

Railscasts - Free Ruby on Rails Screencasts

Re: Help with getting a value from another table

I'm sorry I had it in the controller. It works now as a method in the model, except from storing the real_number in the database. And that is an Important part. So unless it stores it in the Table, it's no point using it. Thanks for all your help Ryan, I so appreciate it.

Last edited by deepti.jerath (2007-03-05 16:22:59)