# Calling Javascript From Model

**URL:** <https://www.ruby-forum.com/t/calling-javascript-from-model/131080>\
**Category:** Rails\
**Created:** [March 5, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/calling-javascript-from-model/131080 "2008-03-05T21:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/calling-javascript-from-model/131080/1 "2008-03-05T21:10:00Z")

</div>

Hello Everyone,

I am pretty new to ROR and am working on my first project which involves  
a pretty sophisticated search. Currently I am performing the search in  
the model. Now for every record in the db I have to do a complicated  
test to see whether or not I should include it in my results. I already  
have the code for the test written in javascript and would like to know  
three things:

a) Can I call the javascript function from within a function in my  
model?  
b) If a) is ok how do I do it?  
c) Is this bad form? Should I instead re-write the complicated test in  
ruby?

Thanks for your input!

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/calling-javascript-from-model/131080/2 "2008-03-05T21:21:40Z")

</div>

this is missing a few details, but anyway:  
no, it’s not possible, to call any js from the model.  
the model will run on the server, the js in the browser…

only way would be ajax (and then you would call it from the view, not  
the model)  
and this would only make for the purpose to update the browser

without knowledge of details:  
any logic that runs on your data to be done in the model  
and that means Ruby

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 5, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/calling-javascript-from-model/131080/3 "2008-03-05T21:32:00Z")

</div>

Thanks thorsten. That makes sense. I will just have to bite the bullet  
and re-write the algorithm in ruby.

Thorsten M. wrote:

> this is missing a few details, but anyway:  
> no, it’s not possible, to call any js from the model.  
> the model will run on the server, the js in the browser…
> 
> only way would be ajax (and then you would call it from the view, not  
> the model)  
> and this would only make for the purpose to update the browser
> 
> without knowledge of details:  
> any logic that runs on your data to be done in the model  
> and that means Ruby
