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

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

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

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

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

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"

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 --

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

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.

Hi Colin

Yes it is my name

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 caught

NoMethodError in PresenterController#create

undefined method `role' for #<Presenter:0x2f3d2dc>

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

plz Guide me

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

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

Fred

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

Hi Fred

Thank you

I did

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 Development 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

Hi Colin

I did my self

and found the solution

Thank you for your advice