Hi all, Not completely sure what I''m doing, but I need to get data from another table based on and id. Below is some breakpointer output: irb> @params => {"project"=>{"project_description"=>"point2", "project_name"=>"break2"}, "action"=>"create_project", "id"=>{"collect"=>"6"}, "controller"=>"adm/project"} The one I want is the ''6'' in "id"=>{"collect"=>"6"} The irb output is right after the form submission. The following statements are executed @contact = Contact.new @project = Project.new(@params[''project'']) @project_contact = ProjectContact.new(@params[''collect'']) breakpoint What needs to be done is catch some fields and copy them based on the id. @project_contact.firstname = @contact.find_on_id_from_form_somehow.firstname The "find_on_id_from_form_somehow" needs to be replaced with a conditional find .. i guess. Can somebody give me a pointer? Thanx a lot. Regards, Gerard -- "Who cares if it doesn''t do anything? It was made with our new Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..." My $Grtz =~ Gerard; ~ :wq!
Jens-Christian Fischer
2006-Jan-09 13:08 UTC
[Rails] catch id from form and copy between objects
> > What needs to be done is catch some fields and copy them based on > the id. > > @project_contact.firstname = > @contact.find_on_id_from_form_somehow.firstname > > The "find_on_id_from_form_somehow" needs to be replaced with a > conditional > find .. i guess. Can somebody give me a pointer?As a long time Lotus Notes developer, I know that "redundancy is good ;-)", but in SQL land, is this really what you want? Is the firstname in @project_contact really independent of the @contact? If not (that''s what I suspect) then you could have something like Class Project belongs_to :contact end Class Contact has_many :projects end and in your code write something like: @project = Project.new(@params....) @project.contact = Contact.find(@params[:collect]) and later project contact firstname: <%= @project.contact.firstname -%> sorry, if this is totally not what you wanted cu jc -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060109/43712ecc/smime.bin
Jens-Christian, There are two contacts tables: company (hm) <-> (bt) contacts project (ho) <-> (bt) project_contacts (ho might become hm) project (ho) <-> (bt) project_products When creating a new project I copy some of the fields from "contacts" to "project_contacts". The following would occcur with only the contacts table: When a contact is destroyed (resigns or starts working for a different company), the project is no longer visible and will generate errors, ''nill'' issues I assume. When I run the new_project method a nice list is created (line 23) based on a sql query (line 21). When submitting the form and going to create_project I get kinda lost. I''m not sure if line 29 is nessecary. And the line from the earlier email:> > @contact.find_on_id_from_form_somehow.firstnameShould be on line 36, but I''m still figuring out how to get the contents of a specific field of a specific table. I''m going to do this for several, but not all, fields. (probably: firstname, lastname, telnumber, email, company name and central company telnumber). Hope you can give me some pointers. Thanx a lot!! Regards, Gerard. 18 def new_project 19 @project = Project.new 20 21 @contacts_list = Contact.find_by_sql(''select t1.name, t2.firstname, t2.lastname, t2.id from companies AS t1, contacts AS t2 where t1.id = t2.company_id order by t1.name'') 22 23 @contacts_list = @contacts_list.collect { |c| [ c.name + " - " + c.firstname + " " + c.lastname, c.id ] } 24 render_action ''edit_project'' 25 26 end 27 28 def create_project 29 @contact = Contact.new 30 @project = Project.new(@params[''project'']) 31 @project_contact = ProjectContact.new(@params[''collect'']) 32 33 @project.project_contact = @project_contact 34 #breakpoint 35 contact = Contact.find(@params[''id''][''collect'']) 36 @project.project_contact[''firstname''] = contact["firstname"] 37 38 #@project.project_contact.save 39 if @project.save 40 flash[:notice] = ''Project was successfully created.'' 41 redirect_to :action => ''show_project'', :id => @project.id 42 else 43 render :action => ''edit_project'' 44 end 45 end On Monday 09 January 2006 14:08, Jens-Christian Fischer tried to type something like:> > What needs to be done is catch some fields and copy them based on > > the id. > > > > @project_contact.firstname > > > > The "find_on_id_from_form_somehow" needs to be replaced with a > > conditional > > find .. i guess. Can somebody give me a pointer? > > As a long time Lotus Notes developer, I know that "redundancy is > good ;-)", but in SQL land, is this really what you want? Is the > firstname in @project_contact really independent of the @contact? If > not (that''s what I suspect) then you could have something like > > Class Project > belongs_to :contact > end > > Class Contact > has_many :projects > end > > and in your code write something like: > > @project = Project.new(@params....) > @project.contact = Contact.find(@params[:collect]) > > and later > > project contact firstname: <%= @project.contact.firstname -%> > > sorry, if this is totally not what you wanted > > cu jc > > -- > InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich > Phone: +41 44 401 09 30 > http://www.invisible.ch http://not.invisible.ch-- "Who cares if it doesn''t do anything? It was made with our new Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..." My $Grtz =~ Gerard; ~ :wq!