# One-to-One Associations and REST

**URL:** <https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528>\
**Category:** Rails\
**Created:** [March 3, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528 "2007-03-03T13:32:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/1 "2007-03-03T13:32:04Z")

</div>

Hello,

I’m trying to write a piece of code that enables users to upload an  
avatar. The user code and the avatar code does work great, but now I  
want to associate the avatar with the user, which should also be  
reflected in the urls.

I’ve read the B-Simple REST tutorial ([http://www.b-simple.de/documents](http://www.b-simple.de/documents))  
which explains this process for one-to-many associations. But things are  
a little different for me, because I only want one avatar per user, I do  
not need an index action and the urls do look different. For example, I  
want /user/:user\_id/avatar to show the user avatar,  
/user/:user\_id/avatar;edit to change the avatar etc.

Is there any way to achieve this?

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/2 "2007-03-03T14:35:46Z")

</div>

Hi,  
I’m still finding my way around RoR myself but isn’t what you want  
something  
like?:

class User \< ActiveRecord::Base  
has\_one :Avatar, :foreign\_key =\> “your\_avatar\_id\_key”,  
end

class Avatar \< ActiveRecord::Base  
belongs\_to :User, :foreign\_key =\> “you\_user\_id\_key”,  
end

–  
Regards

Andrew

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/3 "2007-03-03T15:15:46Z")

</div>

Hello Andrew,

thanks for your reply. Unfortunately this does not help me, because this  
is for the associations only. I need help for reflecting these  
associations in my REST enabled urls.

Andrew M. wrote:

> Hi,  
> I’m still finding my way around RoR myself but isn’t what you want  
> something  
> like?:
> 
> class User \< ActiveRecord::Base  
> has\_one :Avatar, :foreign\_key =\> “your\_avatar\_id\_key”,  
> end
> 
> class Avatar \< ActiveRecord::Base  
> belongs\_to :User, :foreign\_key =\> “you\_user\_id\_key”,  
> end
> 
> –  
> Regards
> 
> Andrew

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/4 "2007-03-03T17:47:16Z")

</div>

> I’ve read the B-Simple REST tutorial ([http://www.b-simple.de/documents](http://www.b-simple.de/documents))  
> which explains this process for one-to-many associations. But things are  
> a little different for me, because I only want one avatar per user, I do  
> not need an index action and the urls do look different. For example, I  
> want /user/:user\_id/avatar to show the user avatar,  
> /user/:user\_id/avatar;edit to change the avatar etc.

map.resources :people do |people|  
people.resource :avatar  
end

avatar\_url(:person\_id =\> person) # =\> /people/5/avatar  
edit\_avatar\_url(:person\_id =\> person) # =\> /people/5/avatar;edit

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/5 "2007-03-03T18:00:17Z")

</div>

on a similar note, what’s generally regarded as best practice for a  
REST-ful multi-lingual site, let’s say one which uses Globalize,  
where the language code has to be passed around in the URL (so that  
pages can be cached easily per language)?

If the first part of the URL always holds the language code, e.g.:

[http://mysite.tld/en/](http://mysite.tld/en/)…  
[http://mysite.tld/de/](http://mysite.tld/de/)…  
[http://mysite.tld/es/](http://mysite.tld/es/)…

How is this declared using resource/resources? An outer map.resource  
which contains all other route mappings?

3 mar 2007 kl. 17.45 skrev DHH:

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [March 3, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/one-to-one-associations-and-rest/87528/6 "2007-03-03T21:08:23Z")

</div>

DHH wrote:

> map.resources :people do |people|  
> people.resource :avatar  
> end
> 
> avatar\_url(:person\_id =\> person) # =\> /people/5/avatar  
> edit\_avatar\_url(:person\_id =\> person) # =\> /people/5/avatar;edit

Great, thanks! I guess I should not have missed this blogpost:

> **[Rails 1.2.2: SQLite3, gems, singular resources](https://rubyonrails.org/2007/2/6/rails-1-2-2-sqlite3-gems-singular-resources)**
>
> It’s time for another minor update to Rails 1.2. This was primarily prompted by a change in the API for SQLite between version 3.3.7 and 3.3.8+, which left the Rails database adapter for dead by the road side. But with this release and Jamis Buck’s...

One last thing to mention: The avatar controller has to be singular,  
too. This means the controller name (AvatarController) and the file name  
(avatar\_controller.rb). I missed the file name first, which took me  
quite some time to fiddle out…
