# Ensuring AR#set\_table\_name is valid with multiple users

**URL:** <https://www.ruby-forum.com/t/ensuring-ar-set-table-name-is-valid-with-multiple-users/118308>\
**Category:** Rails\
**Created:** [October 25, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/ensuring-ar-set-table-name-is-valid-with-multiple-users/118308 "2007-10-25T15:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [October 25, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/ensuring-ar-set-table-name-is-valid-with-multiple-users/118308/1 "2007-10-25T15:16:00Z")

</div>

Hi,

due to the legacy nature of my database I have to do a  
set\_table\_name() to make sure that the correct MySQL table is picked  
up by the AR model.

For example:

model.set\_table\_name(params[:use\_this\_table])

One thing that occurred to me is that if I have multiple users  
accessing my app and they’re looking at different tables then I run  
the risk that one user’s set\_table\_name might over-write another’s  
leading to incorrect data being sent to the user.

To mitigate the problem I do the set\_table\_name as close as possible  
to the find() or where I write to the database.

Is this a valid concern? Are there more techniques to prevent this  
happening or even detect that it happens?

Thanks,

Allan

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [October 25, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/ensuring-ar-set-table-name-is-valid-with-multiple-users/118308/2 "2007-10-25T18:07:00Z")

</div>

I’d see this as a HUGE design mistake. That is so thread un-safe.  
Imagine rails being thread-safe in next 6 months, and you’ll still be  
stuck with a big fat lock around your request.

I’m sure there are other ways to achieve whatever it is you’re trying  
to, but this is not a right way. Go with the solution which doesn’t  
change global behavior of your models during request serving.

Probably you might end up having 100 models. Which is just fine.

params[:use\_this\_model].classify.find(:all) = GOOOOOD  
model.set\_table\_name(params[:use\_this\_table]) && model.find(:all) =  
BAD/FUGLY

On 10/25/07, Allan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,
> 
> Allan
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [October 25, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/ensuring-ar-set-table-name-is-valid-with-multiple-users/118308/3 "2007-10-25T20:13:12Z")

</div>

Allan wrote:

> Hi,
> 
> due to the legacy nature of my database I have to do a  
> set\_table\_name() to make sure that the correct MySQL table is picked  
> up by the AR model.
> 
> For example:
> 
> model.set\_table\_name(params[:use\_this\_table])

[…]

> To mitigate the problem I do the set\_table\_name as close as possible  
> to the find() or where I write to the database.

Another option might be to construct AR classes as needed:

ar\_class = SpecialARConstructor.create\_class(params[:use\_this\_table])  
ar\_class.find(…)  
new\_row = ar\_class.new(…)  
new\_row.save  
etc.

The class definition of the classes created would invoke the desired  
set\_table\_name.

You would need to use the dynamic features of Ruby when implementing the  
create\_class method in the SpecialARConstructor (module?)

Good luck!

Stephan
