SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to use model in application controler?

    I want to use my User model in my application controller for a global method... how would I make it so it can use the User model? Thanks for the help guys.
    Have a good day.

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create objects based on the class. Below are three examples based on this model object
    Code:
    class Thing < ActiveRecord::Base
      #an instance method
      def return_one
        1
      end
    
      #A class method (could also be defined via Thing.return_two)
      def self.return_two
        2
      end
    end
    And use of this model's code elsewhere (including the application.rb)
    Code:
      #Use of the instance method
      #First grab an instance of a User object e.g find the first user
      @user = User.find(:first)
      answer_one = @user.return_one
    
      #This would also work
      @user = User.new
      answer_one = @user.return_one
    
      #Use the class method
      @answer_two = User.return_two

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It gives me errors that User is an undefined constant...

    OOPS! My bad a naming error...
    Have a good day.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •