# Accessing request from model (or observer)

**URL:** <https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792>\
**Category:** Rails\
**Created:** [April 24, 2007, 9:19am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792 "2007-04-24T09:19:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [April 24, 2007, 9:19am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792/1 "2007-04-24T09:19:41Z")

</div>

Hello,

I am currently developing an engine which allows several different  
sites to be served using the same code. The implementation is like  
this:

- every Post has a site\_id field, which is set to the ID is the site  
to which the post belongs to.
- in Apache config, I have something like:  
ServerName [site1.com](http://site1.com)  
ServerAlias [site2.com](http://site2.com)

[site1.com](http://site1.com) and [site2.com](http://site2.com) are different sites, with ID’s 1 and 2. When a  
user accesses the site, engine detects which url is used (by comparing  
request.host against sites database) and serves only posts with the  
corresponding site\_id. @current\_site variable is set on application  
controller level before any page is served to user.

The question:  
I want to make the process of creating posts as clean as possible.  
Meaning that when a post is saved, site\_id is automatically attached  
to it. The best possible way would be to do it in post model’s  
before\_save function, but the problem is that neither @current\_site  
variable, nor request is available to the post model.  
I tried to use observers with no luck (same access problems as with  
model)

Any ideas or may be some fresh approaches are very appreciated. I  
really would like to leave setting site\_id in controller as a last  
resort (doesn’t take much time to implement, but just oh so un-  
clean 🙂

TIA,  
Mike.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [April 24, 2007, 9:57am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792/2 "2007-04-24T09:57:45Z")

</div>

Just set the site\_id in the controller before calling post.save.

b

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [April 24, 2007, 10:16am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792/3 "2007-04-24T10:16:19Z")

</div>

Thanks for the great idea Ben!

That’s how I have implemented it (is it the way you meant it?). All  
controllers in admin interface have AdminController as a parent so I  
define the “fix” on the topmost level.

class AdminController \< ApplicationController  
…  
before\_filter :set\_site\_id, :only =\> [:create, :update]

private  
def set\_site\_id()  
params[:page][:site\_id] = @current\_site.id  
end  
…

I wonder if there’s a better way? Or the current approach is good  
enough. I don’t really like to juggle with params array manually.

mike.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [April 24, 2007, 11:09am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792/4 "2007-04-24T11:09:34Z")

</div>

Looks like the approach above solves the problem with setting site\_id  
on save, but it doesn’t solve the problem overall.

I do need some way to access either @params or a variable set inside  
the action controller. That’s because I want to modify model’s default  
find method so all search would return only records which belong to  
current site.

Currently I have the following find method which overrides the default  
find in the model:

def self.find(\*args)  
with\_scope(:find =\> { :conditions =\> “posts.post\_type=‘page’” })  
do  
super  
end  
end

What I would like to be able to do it:

def self.find(\*args)  
with\_scope(:find =\> { :conditions =\> [“posts.post\_type=‘page’ and  
posts.site\_id=?”, @current\_site\_id] }) do  
super  
end  
end

Where @current\_site\_id is defined in action controller (that’s where  
the @request array becomes available).  
Any ideas?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [April 25, 2007, 12:54am UTC](https://www.ruby-forum.com/t/accessing-request-from-model-or-observer/92792/5 "2007-04-25T00:54:15Z")

</div>

Mikhail Kornienko wrote:

> ```
> with_scope(:find => { :conditions => ["posts.post_type='page' and
> 
> ```
> 
> posts.site\_id=?", @current\_site\_id] }) do  
> super  
> end  
> end
> 
> Where @current\_site\_id is defined in action controller (that’s where  
> the @request array becomes available).  
> Any ideas?

Well, find has \*args to play with… maybe just pass the current\_site\_id  
to find. Don’t know if having an extra arg in the array will make the  
call to super puke, but you can always remove it before calling super.

b
