How to call method declared in one model

hi guys

I need to know how to call one method declared in one model to another
controller or another model

e.g

model1.rb

def check1
puts “check1”
end

model2.rb

def check2
puts “check2”
end

i need to access check2 method from check1
in model1.rb my line to access check2 method is

model2.check2

but i am not getting this

should i need to do any thing
plz guide me


Karthik.k
Mobile - +91-9894991640

2009/8/31 karthik k [email protected]:

puts “check1”

model2.check2

I believe that You cannot call a controller model from another
controller in this way. If you need to do this it probably means that
the method should be in a model or in a module in the lib directory.
Or are you actually trying to redirect to a different action rather
than just call a method?

Colin

You can call like model2.check2, if the method check2 is a class level
method.

Write method like below. So that you can this method using
model2.check2.

def self.check2
puts “check2”
end

On Mon, Aug 31, 2009 at 3:33 PM, Colin L. [email protected]
wrote:

model1.rb

than just call a method?

Sorry I misread the question and the reply above is rubbish anyway. I
thought you were wanting to call a controller method from another
controller. I must remember to engage brain before typing.

Colin

hi Colin

That is not a problem

You have the mind to help that is far more enough even if you misjudged

Thank you guys

You save me


Karthik.k
Mobile - +91-9894991640

2009/8/31 Colin L. [email protected]:

def check1
in model1.rb my line to access check2 method is

model2.check2

I believe that You cannot call a controller model from another
controller in this way. Â If you need to do this it probably means that
the method should be in a model or in a module in the lib directory.
Or are you actually trying to redirect to a different action rather
than just call a method?

Sorry I misread the question and the reply above is rubbish anyway. I
thought you were wanting to call a controller method from another
controller. I must remember to engage brain before typing.

Colin

If you want to call check1 from another model/controller then you can do
any
of the following:

=> make check1 class method

model1.rb

def self.check1
end

model2.rb

def check2.rb
Model1.check1
end

=> or if you want to keep it as a instance method (or it depends on the
state of object)

model1.rb

def check1
end

model2.rb

def check2.rb
@model1.check1
end


Thanks,
Abhinav
http://twitter.com/abhinav

On Mon, Aug 31, 2009 at 3:38 PM, karthik k [email protected]
wrote:

controller or another model

I believe that You cannot call a controller model from another
Colin
You save me


Karthik.k
Mobile - +91-9894991640

Hi Guys

This is anew Question

i am writing a method in model

In check1.rb //model
def check?
self.name=“karthik”
end

when i call from controller say check2controller.rb //different
controller

i am calling as
check1=check1.new
check1.check? //returns true or false

i need to pass karthik as paramater to check?in check1.rb
so that it will return true or false

Please guide me how to do

Karthik.k
Mobile - +91-9894991640
http://kkarthikresume.blogspot.com/

2009/8/31 karthik k [email protected]:

On Mon, Aug 31, 2009 at 3:38 PM, karthik k [email protected] wrote:
This is anew Question

i am writing a method in model

In check1.rb //model
def check?
self.name=“karthik”

I think that should be self.name == “Karthik”

Please guide me how to do

def check?( a_name )
self.name == a_name
end

Then check1.check?( “karthik” )

If you call it immediately after check1.new then the name will always
be the default one for the name attribute of course. check1.check?(
“karthik” ) means hey object check1, is your name karthik?

Colin

On Mon, Aug 31, 2009 at 4:04 PM, Colin L. [email protected]
wrote:

In check1.rb //model
i am calling as
self.name == a_name
end

Then check1.check?( “karthik” )

If you call it immediately after check1.new then the name will always
be the default one for the name attribute of course. check1.check?(
“karthik” ) means hey object check1, is your name karthik?

Colin

Hi Colin

Yes it is my name


Karthik.k
Mobile - +91-9894991640

Hi Karthik,

There are a lot of mistakes (bug) in your code, but I think
philosophically
too you are wrong.

Firstly, you have “=” instead of “==” in your check? method. And if the
word
“karthik” is constant you don’t need to pass it. I think this solves
your
problem.

But please do read some detailed tutorial or preferably a book on rails,
because I think you are missing too many points here.

अभिनव
http://twitter.com/abhinav

On Mon, Aug 31, 2009 at 4:17 PM, Colin L. [email protected]
wrote:

On Mon, Aug 31, 2009 at 3:38 PM, karthik k [email protected]

so that it will return true or false
If you call it immediately after check1.new then the name will always
I think we are loosing something in the translation in this conversation.

Colin

Hi

Same old model problem

role.rd //model
def check_role
puts “Role sdfsdfd”
end

presenter.rb //model
def validate_name
self.role.check_role //calling from role.rb model
end

in presenter_controller i am calling as

@presenter= Presenter.new
@presenter.validate_name

i am getting as

Action Controller: Exception caughtNoMethodError in
PresenterController#create

undefined method `role’ for #Presenter:0x2f3d2dc

but in the existing code it is working for some other module

plz Guide me


Karthik.k
Mobile - +91-9894991640

2009/8/31 karthik k [email protected]:

when i call from controller say check2controller.rb //different

Colin

Hi Colin

Yes it is my name

I think we are loosing something in the translation in this
conversation.

Colin

def validate_name
Action Controller: Exception caughtNoMethodError in
PresenterController#create

undefined method `role’ for #Presenter:0x2f3d2dc

but in the existing code it is working for some other module

Does Presenter have a role method ? if not then you’ve got your
answer.

Fred

On Tue, Sep 1, 2009 at 9:24 AM, Frederick C.
<[email protected]

wrote:

presenter.rb //model

hi Fred

I am not able to under stand
Does Presenter have a role method ?
no there is no method in Presenter model

can you plz explain in detail


Karthik.k
Mobile - +91-9894991640

Fred

On Tue, Sep 1, 2009 at 9:39 AM, karthik k [email protected]
wrote:

role.rd //model

but in the existing code it is working for some other module
no there is no method in Presenter model

can you plz explain in detail


Karthik.k
Mobile - +91-9894991640

Hi Fred

Thank you

I did


Karthik.k
Mobile - +91-9894991640

On Tue, Sep 1, 2009 at 2:32 PM, Colin L. [email protected]
wrote:

answer.
Karthik, I have a two suggestions which I would like you consider very
This may seem like a lot of wasted time but in the long run it will
save you (and us) time and effort and will allow you develop reliable
applications much more effectively than you can at the moment.

Colin

Hi Colin

I did my self

and found the solution

Thank you for your advice


Karthik.k
Mobile - +91-9894991640

2009/9/1 karthik k [email protected]:

hi Fred

I am not able to under stand
Does Presenter have a role method ?
no there is no method in Presenter model

can you plz explain in detail

Karthik, I have a two suggestions which I would like you consider very
seriously.

  1. Get hold of the Pragmatic Bookshelf book Programming Ruby: The
    Pragmatic Programmer’s Guide, downloadable as an e-book. Spend a week
    working through it till you have a good grasp of Ruby.

  2. Get the book Agile Web D. with Rails, 3rd Edition and
    spend another week (or more) working through that to give you a good
    grasp of Rails.

This may seem like a lot of wasted time but in the long run it will
save you (and us) time and effort and will allow you develop reliable
applications much more effectively than you can at the moment.

Colin