Topic: Need to call username variable into Model from Controller

I have two models, one is user.rb and the other link.rb.
The user.rb handles my authentication which is working but I need to get the current username varible into my link.rb.

I am accessing the current username now in my user/welcome.rhtml file with this code:
           You are logged in as <%= session[:user].login %>


What I need is to get this variable into my link.rb file which currently looks like this:

class Link < ActiveRecord::Base
   def self.return_links
   @my_data = find(:all, :conditions => "username = #{CURRENT USER NAME GOES HERE SOMEHOW}")
  end
end

Any help would be appreciated. I've been working on this for a week and have tried every possible variation.

Thanks....John

Re: Need to call username variable into Model from Controller

I'm assuming that session[:user] is a User instance stored in the session, is that right?

A common way of doing this would go like this:

your view:

  <%  for link in Link.links_for(session[:user]) do %>
    <%= link %>
  <%  end %>

in link.rb:
class Link < ActiveRecord::Base
  def self.links_for(user)
    find_all_by_username(user)
  end
end

Re: Need to call username variable into Model from Controller

thanks for the help!

Yes, session[:user] is stored in the session after login but I can't seem to even call it in a puts statement from my link.rb or user.rb for some reason.

I tried you code but I had to add it toward the bottom of my link.rb since I have other methods relying on the MySQL DB call in self.return_links and it didn't work out. I got no errors, but still can't pass the username.

Just to get back to basics, as it stands right now with my existing code, shouldn't I be able to print the variable out of link.rb with:

puts "This is the variable: #{session[:user].login}"

Re: Need to call username variable into Model from Controller

The session hash is actually a highly customized object.  It's like a hash with some special extensions.  It's only available to the controllers and the views.  If you're inside a model you can't see the session variables.  This kind of stuff is bothersome but it's one of the strengths of Rails.

Can you post your Link class?  Maybe together we could get it set up so it works smoothly.

Re: Need to call username variable into Model from Controller

You are a great man for taking the time!
Here is link.rb:

class Link < ActiveRecord::Base
        def self.return_links
puts "THE USER IS: #{I WAS TRYING TO PRINT OUT THE USERNAME VARIABLE HERE WITH NO LUCK}"
        @data = find(:all, :conditions => "username = #{USERNAME VARIABLE MUST GO HERE}")
        end
end

Re: Need to call username variable into Model from Controller

You mentioned there are other methods that rely on the mysql db call in self.return_links.  Are those in the model as well or are they elsewhere?

Re: Need to call username variable into Model from Controller

I have a manage controller that uses links.rb to pull a list of from the DB based on username as well

Re: Need to call username variable into Model from Controller

Ahh, then you should be able to use the following link.rb:

class Link < ActiveRecord::Base
  def self.links_for(user)
    find_all_by_username(user.username)
  end
end

And do this in your ManageController:

class ManageController < ActionController::Base
  def index
    @links = Link.links_for(session[:user])
  end
end

Does that work?

Re: Need to call username variable into Model from Controller

I actually have three controllers user, buy and manage. Buy and manage are almost identical and I know if I can get buy working, manage will follow. I made the changes you suggested in link.rb and the buy controller. I'm getting the following error from the buy controller:
undefined method `username' for #<User:0x40a47604>
thanks Danger..

Re: Need to call username variable into Model from Controller

Huh, it looks like you don't have a column in your users table called 'username'.  If you replace:
find_all_by_username(user.username)
with
find_all_by_username(user.name)
or
find_all_by_username(user.login)
or whatever you use in the users table to store the username then the code should work.

good luck!

Re: Need to call username variable into Model from Controller

Danger.....I owe you my life for that! ...thanks SO much for the help

Everything works great....now on to my next stumbling block :-)

Take care....if you ever need any networking help....let me know: jackster@fundwatch.net

Re: Need to call username variable into Model from Controller

Danger, one last thing that I didn't expect, now that I am limiting the DB query at the controller, I have a "def create" in my manage_controller that needs to specify the currently logged in user as well when creating new links. The method is:

def create
    @link = Link.new(params[:link])
    if @link.save
      flash[:notice] = 'Link was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end

I know that this method relies on the "commit" scaffold method and is looking for this:
Parameters: {"commit"=>"Create", "link"=>{"graph"=>"MEFAX", "username"=>""}}

How do I pass the newly constructed session:[user] to scaffold where it's looking for username in @link = Link.new(params[:link]) ? Do I have to somehow edit where commit is called in scaffold?

thanks again

Re: Need to call username variable into Model from Controller

All you should need is to add one line in your controller's action.  I'm not sure what you named your relationship between Link and User in their respective models, so I'm going to assume your app will end up like this

#in link.rb:
class Link < ActiveRecord::Base

  belongs_to :user # you'll need to add a user_id field to the links table

  def self.links_for(user)
    find_all_by_username(user.username)
  end

end

#in user.rb:
class User < ActiveRecord::Base
  has_many :links # you don't need to add any fields to the user table for this
end

#in manage_controller.rb
class ManageController < ActionController::Base
  def create
    @link = Link.new(params[:link])
    # the following line automatically sets the link's user_id to the session[:user]'s id
    @link.user = session[:user]
    if @link.save
      flash[:notice] = 'Link was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end
end


That should do it.

Re: Need to call username variable into Model from Controller

Danger, your code works flawlessly but deviates from the other methods in my manage controller.
The way I have it setup now, you create a user id and then you manually enter financial graph symbols for graph data that you want to see, everything is indexed from the login name (named login in the db). The code you suggested creates a user_id for the create method but it doesn't get tied to the login name so when I click on "list", it never shows up for that user because "list" is looking for all graphs for a certain username (the piece you got working for me). Perhaps if I can get "list" in manage to list by user_id, I will be all set.

I created a login to the site for you username and password is danger/password. The url is
http://www.fundwatch.net:3000/user/login. If you want to add or delete graphs, it works as long as you put your username in everytime and choose either of these symbols MEFAX or JAOSX.

Thanks Danger....