This weekend, I have a written my first Rails application to handle technical reports at school. The model is very simple, and the only part I don''t know how to deal with is the association between authors and reports. Each report has many authors and each author has many reports. I have changed this many-to-many relationship into a one-to-many relationship adding the following table: CREATE TABLE publication_authors ( id INTEGER PRIMARY KEY, author_id INTEGER, publication_id INTEGER ); Is this really necessary with Rails? Any other suggestions? Right now, the edit view for the reports has a table with all the authors in the database and a checkbox for each one of them. If the list of authors grows too big, this part is going to be a bit too rough and unusable. How can I handle this "The Rails Way"? I thought a very neat solution would be to use an input field like the ones GMail has for the address book. Is that difficult to implement using Rails and Ajax? Any ideas on how to do that? Are any ActionView helpers available to do this? How can I dinamically add more input fields (pushing an "add author" button)? In case this solution is way too far from being done, how can I handle checking and unchecking the different authors in that table on the Controller side? What I would need to do is remove the unchecked authors and add the checked ones. This involves iterating over the collection of authors and checkboxes and compare the previous values with the current ones (which may render the code a little bit complicated). How do you all usually solve this problem? Is there any solution for this available in Ruby? Thanks to everyone, Ed -- Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com "Tener una amiga en Ginebra es como tener quinotos en almibar o uvas en ron." "Programming is like sex... make one mistake, and support it the rest of your life." "Defeat is an accomplishment not even the best of us could achieve."
Hi, I''m not in a position to comment on the UI issues you bring up but strictly from a DB perspective: why did you change the M:N to 1:N? Did you do so because you aren''t aware of has_and_belongs_to_many? Trevor On 25-Apr-05, at 10:28 PM, Edgardo Hames wrote:> This weekend, I have a written my first Rails application to handle > technical reports at school. The model is very simple, and the only > part I don''t know how to deal with is the association between authors > and reports. > > Each report has many authors and each author has many reports. I have > changed this many-to-many relationship into a one-to-many relationship > adding the following table: > > CREATE TABLE publication_authors ( > id INTEGER PRIMARY KEY, > author_id INTEGER, > publication_id INTEGER > ); > > Is this really necessary with Rails? Any other suggestions? > > Right now, the edit view for the reports has a table with all the > authors in the database and a checkbox for each one of them. If the > list of authors grows too big, this part is going to be a bit too > rough and unusable. How can I handle this "The Rails Way"? > > I thought a very neat solution would be to use an input field like the > ones GMail has for the address book. Is that difficult to implement > using Rails and Ajax? Any ideas on how to do that? Are any ActionView > helpers available to do this? How can I dinamically add more input > fields (pushing an "add author" button)? > > In case this solution is way too far from being done, how can I handle > checking and unchecking the different authors in that table on the > Controller side? What I would need to do is remove the unchecked > authors and add the checked ones. This involves iterating over the > collection of authors and checkboxes and compare the previous values > with the current ones (which may render the code a little bit > complicated). How do you all usually solve this problem? Is there any > solution for this available in Ruby? > > Thanks to everyone, > Ed > > -- > Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com > > "Tener una amiga en Ginebra es como tener quinotos en almibar o uvas > en ron." > "Programming is like sex... make one mistake, and support it the rest > of your life." > "Defeat is an accomplishment not even the best of us could achieve." > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 4/26/05, Trevor Squires <trevor-k8q5a0yEZAgS+FvcfC7Uqw@public.gmane.org> wrote:> Hi, > > I''m not in a position to comment on the UI issues you bring up but > strictly from a DB perspective: why did you change the M:N to 1:N? > > Did you do so because you aren''t aware of has_and_belongs_to_many? >If I correctly understand the documentation, when I use "has_and_belongs_to_many" I still need the join table authors_publications (author_id, publication_id), but it is automatically handled by Ruby. Do I need to create a model for it? Cheers, Ed -- Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com "Tener una amiga en Ginebra es como tener quinotos en almibar o uvas en ron." "Programming is like sex... make one mistake, and support it the rest of your life." "Defeat is an accomplishment not even the best of us could achieve."
On 4/27/05, Edgardo Hames <ehames-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4/26/05, Trevor Squires <trevor-k8q5a0yEZAgS+FvcfC7Uqw@public.gmane.org> wrote: > > Hi, > > > > I''m not in a position to comment on the UI issues you bring up but > > strictly from a DB perspective: why did you change the M:N to 1:N? > > > > Did you do so because you aren''t aware of has_and_belongs_to_many? > > > > If I correctly understand the documentation, when I use > "has_and_belongs_to_many" I still need the join table > authors_publications (author_id, publication_id), but it is > automatically handled by Ruby. Do I need to create a model for it?You still need the table, but no, you don''t need to create a model. @author.publications and @publication.authors will ''just work''.> Cheers, > Ed > -- > Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com > > "Tener una amiga en Ginebra es como tener quinotos en almibar o uvas en ron." > "Programming is like sex... make one mistake, and support it the rest > of your life." > "Defeat is an accomplishment not even the best of us could achieve." > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On 4/26/05, Edgardo Hames <ehames-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Right now, the edit view for the reports has a table with all the > authors in the database and a checkbox for each one of them. If the > list of authors grows too big, this part is going to be a bit too > rough and unusable. How can I handle this "The Rails Way"? > > I thought a very neat solution would be to use an input field like the > ones GMail has for the address book. Is that difficult to implement > using Rails and Ajax? Any ideas on how to do that? Are any ActionView > helpers available to do this? How can I dinamically add more input > fields (pushing an "add author" button)? >Just in case it can help somebody else, I have found a way to dinamically add a field to a form. If somebody can help with dinamically grabbing the authors as the user types in the input field, I would really appreciate it. Here is the code: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Test Page</title> <script type="text/javascript"> <!-- var textNumber = 1; function addTextBox(form, afterElement) { var label = document.createElement("label"); var textField = document.createElement("input"); textField.setAttribute("type","text"); textField.setAttribute("name","txt"+(++textNumber)); label.appendChild(document.createTextNode("Text Box #"+textNumber+": ")); label.appendChild(textField); form.insertBefore(label,afterElement); return false; } //--> </script> <style type="text/css"> <!-- label { display:block; margin:.25em 0em; } --> </style> </head> <body> <form id="myForm" method="get" action="./pru-test.html" /> <label>Text Box #1: <input type="text" name="txt1" /></label> <p><input type="button" value="Add Textbox" onclick="addTextBox(this.form,this.parentNode)" /></p> </form> </body> </html> Cheers, Ed -- Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com "Tener una amiga en Ginebra es como tener quinotos en almibar o uvas en ron." "Programming is like sex... make one mistake, and support it the rest of your life." "Defeat is an accomplishment not even the best of us could achieve."