Dung Nguyen quang
Dung Nguyen quang

Reputation: 135

How to pass values from method in model to another method, or controller in ruby on rails

Can i pass values between methods in model or between method and controller in ruby on rails?, here my example:

My model:

class Artist::Data < ActiveRecord::Base
   def self.set1(timeline)
      a = timeline
      set2 << a
   end
   def self.set2
      logger.debug "HAA #{a}"
      return a
   end
end

My controller:

class Feed::FeedsController < ApplicationController
  def index
     #get a from set2 method
     @a = Artist::Data.set2  (=a)
  end
end

It's just an example, I don't know how to do it in real app. I want to pass value from set1 method to set2 method, then I can show it in FeedsController. So, how can I do it??, please help me!

Upvotes: 0

Views: 61

Answers (1)

Sony Mathew
Sony Mathew

Reputation: 2971

I assume you are familiar with class variables and instance variables. So if you want to pass data between two class methods you can use class variables.

class Artist::Data < ActiveRecord::Base
   def self.set1(timeline)
      @a = timeline
   end
   def self.set2
      logger.debug "HAA #{@a}"
      return @a
   end
end

More Info : http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/113-class-variables

Upvotes: 1

Related Questions