# CRUD, REST and associations

**URL:** <https://www.ruby-forum.com/t/crud-rest-and-associations/100148>\
**Category:** Rails\
**Created:** [July 27, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/crud-rest-and-associations/100148 "2006-07-27T12:13:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [July 27, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/crud-rest-and-associations/100148/1 "2006-07-27T12:13:18Z")

</div>

Let’s say I have the model class Reader and Magazine, connected by join  
model Subscription. It looks something like this

class Reader \< ActiveRecord::Base  
has\_many :subscriptions, :dependent =\> :delete\_all  
has\_many :magazines, :through =\> :subscriptions  
validates\_presence\_of :name  
end

class Magazine \< ActiveRecord::Base  
has\_many :subscriptions, :dependent =\> :delete\_all  
has\_many :readers, :through =\> :subscriptions  
end

class Subscription \< ActiveRecord::Base  
belongs\_to :reader  
belongs\_to :magazine  
validates\_presence\_of :reader, :magazine  
end

Now, the nascent RESTful orthodoxy appears to be that for each of these  
model classes there ought to be a controller that manages its CRUD  
operations. I admit that this has a nice ring to it, alas, it doesn’t  
seem to mesh with what I’d like to do user interface-wise. Simply put,  
I want to edit and update a reader’s name _and_ their subscriptions in  
one form. The point is that the user saves (or discards) these changes  
in a single user-level transaction. What I don’t want is that changes  
to subscriptions are saved immediately, whereas changes to attributes,  
such as name, are change only on submitting the form. Also, I don’t  
want to spread editing of attributes and editing of subscriptions over  
separate views.

So, within the editing form (\_form.rhtml when spit out by the scaffold  
generator), I display a table of checkboxes and magazines where  
subscriptions are shown and can’t be changed.

\<%= hidden\_field\_tag reader[magazine\_ids][]', ‘’ %\>

\<% for magazine in reader.magazines %\> \<% end %\>

| |
| \<%= check\_box\_tag 'reader[magazine\_ids][]', magazine.id, @reader.subscribed\_to?(magazine) %\> | \<%=h (magazine.title) %\> |

As the Rails API docs state, has\_many :through associations are  
read-only. Instead, changes are handled through the join model. So,  
let’s ameliorate this shortcoming

class Reader  
def magazine\_ids=(ids)  
Reader.transaction do  
subscriptions.clear  
ids.each do |magazine\_id|  
subscription.create(:reader\_id =\> id, :magazine\_id =\> \_id)  
unless magazine\_id.blank?  
magazines(true) # force a reload  
end  
end  
end  
def subscribed\_to?(magazine)  
subscriptions.any? { |s| s.magazine\_id == magazine.id }  
end  
end

Okay, this way it works. Still, surprisingly, it is more effort than  
could be expected as Rails doesn’t support this behavior out of the box  
with a suitable helper and \<association\_singular\_name\>\_ids= methods.  
This _may_ be an indication that something is wrong. Is it? Is there a  
better way, more in keeping with CRUD and REST to achieve the same  
ends?

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
