Hi all, For a dropdownlist (showing "Company - FirstName Lastname'') I want to fill an object @project_contacts with "Name" from table Companies and "Firstname" and "Lastname" from table contacts. Any idea? 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!
Hi all, Watery eyes ... Nevermind the previous. I don''t see that working for me. This is what I want. I create a new project, and want to copy some contact and company info into a project table. I don''t see a link (selected via a view) between to completely seperate tables. Let alone get data from one to the other. Please help? Gerard. On Thursday 05 January 2006 13:49, Gerard tried to type something like:> Hi all, > > For a dropdownlist (showing "Company - FirstName Lastname'') I want to fill > an object @project_contacts with "Name" from table Companies and > "Firstname" and "Lastname" from table contacts. Any idea? > > 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!
Chris Hall
2006-Jan-05 13:34 UTC
[Rails] HOWTO: Combine fields from 2 two tables in 1 object
assuming certain relationships between projects/contacts/companies projects (hm) <--> (bt) contacts (bt) <--> (hm) companies controller: @project.find(1) @project_contacts = @project.contacts.collect { |c| [ c.company.name + " - " + c.first_name + " " + c.last_name, c.id ] } # @project_contacts => [ [ "Acme -Bob Smith", 1], ["MegaCorp - Jane Doe", 2], ... ] view: <%= select("project", "contact_id", @project_contacts ) %> On 1/5/06, Gerard <mailing@gp-net.nl> wrote:> > Hi all, > > For a dropdownlist (showing "Company - FirstName Lastname'') I want to fill > an > object @project_contacts with "Name" from table Companies and "Firstname" > and > "Lastname" from table contacts. Any idea? > > 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! > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060105/6f6801fe/attachment.html
Chris, Hurtfully I must state being a newbie can slow things down .. :-) Cudo''s on your solution. It hits the nail on the head. Looking at your solution .. .would it be wise to buy the pragmatic ruby book. Or do I just need to keep going knowing I''ll get better? Anyway ...> projects (hm) <--> (bt) contacts (bt) <--> (hm) companiesVery close. This is true:(bt) contacts (bt) <--> (hm) companies And there is: projects (ho) <--> (bt) project_contacts The reason is that when a contact resigns (is deleted) I still want to be able to view closed projects. Herewith avoiding the (for me) infamous ''nil'' errors. Since there''s no relationship between projects & project_contacts on one side and contacts on the other. Looks like I need to do a second object fill when saving the new project. I can then use the ''"c.id'' that comes back from @project_contacts and use that id to gather the info for storing it. Or is that info still avalaible in @project.contacts.collect ? Thanx so much for helping me out. Gerard> > controller: > > @project.find(1) > @project_contacts = @project.contacts.collect { |c| [ c.company.name + " - > " + c.first_name + " " + c.last_name, c.id ] } > # @project_contacts => [ [ "Acme -Bob Smith", 1], ["MegaCorp - Jane Doe", > 2], ... ] > > view: > > <%= select("project", "contact_id", @project_contacts ) %> > > On 1/5/06, Gerard <mailing@gp-net.nl> wrote: > > Hi all, > > > > For a dropdownlist (showing "Company - FirstName Lastname'') I want to > > fill an > > object @project_contacts with "Name" from table Companies and "Firstname" > > and > > "Lastname" from table contacts. Any idea? > > > > 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! > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-- "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!
Chris, Having chewed on the line bewol some more @project_contacts = @contacts_list.collect { |c| [ c.name + " - " + c.firstname + " " + c.lastname, c.id ] } I do need the c.id in the end. but not displayed in the dropdownlist. And it''s shown like this now: "Company - FirstName Lastname3" The "3" being the id of the contact. Do i need to fill the @project_contacts in a different way, or address the select statement data input in a different way? What would be your choice for ease of use? Thanx, Regards. On Thursday 05 January 2006 14:34, Chris Hall tried to type something like:> assuming certain relationships between projects/contacts/companies > > projects (hm) <--> (bt) contacts (bt) <--> (hm) companies > > controller: > > @project.find(1) > @project_contacts = @project.contacts.collect { |c| [ c.company.name + " - > " + c.first_name + " " + c.last_name, c.id ] } > # @project_contacts => [ [ "Acme -Bob Smith", 1], ["MegaCorp - Jane Doe", > 2], ... ] > > view: > > <%= select("project", "contact_id", @project_contacts ) %> > > On 1/5/06, Gerard <mailing@gp-net.nl> wrote: > > Hi all, > > > > For a dropdownlist (showing "Company - FirstName Lastname'') I want to > > fill an > > object @project_contacts with "Name" from table Companies and "Firstname" > > and > > "Lastname" from table contacts. Any idea? > > > > 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! > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-- "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!
Hi Chris, After a lot of ruby studying (and some frustration whilest not going fast enough .. :-) I cleaned up my code somewhat and all seems better and makes more sence now. I was wondering if you could do me one (no guarantees though) more favour on the code: Controller: @project = Project.new @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'') @contacts_list = @contacts_list.collect { |c| [ c.id, c.name + " - " + c.firstname + " " + c.lastname ] } View (new_project): # This one makes sence, However it comes up with an empty list. <!-- %= select_tag "id", @contacts_list % --> # This one works and shows a list of "company - contacts <%= collection_select("contacts", "id", @contacts_list, :id, "collect", { :include_blank => true } ) Based on the id from the contact dropdown list I want to copy contact info from the Contact model to the ProjectContact model when invoking "create_project". Can you give me a pointer on how to dig up (debug) what info is availarble on form submission? Thanx again. On Friday 06 January 2006 14:32, your brain produced:> actually, i screwed up the example, as project has_many contacts, therefore > there is no contact_id in projects table. so the <%= select "project", > "contact_id", ... %> example is erroneous. > > what you probably want is > > controller: > @project = Project.find(1, :include => :contacts) > @contacts_for_select = options_for_select(@project.contacts.collect { |c| [ > c.company.name + " - " + c.f_name + " " + c.l_name, c.id ] }) > > view: > **<%= select_tag "contact_id", @contacts_for_select %> > > this will give you params[:contact_id] when you submit the form > > sorry about the confusion > > Chris > > On 1/6/06, Gerard <mailing@gp-net.nl> wrote: > > Chris, > > > > Having chewed on the line bewol some more > > > > @project_contacts = @contacts_list.collect { |c| [ c.name + " - " + > > c.firstname + " " + c.lastname, c.id ] } > > > > I do need the c.id in the end. but not displayed in the dropdownlist. And > > it''s > > shown like this now: "Company - FirstName Lastname3" > > The "3" being the id of the contact. Do i need to fill the > > @project_contacts > > in a different way, or address the select statement data input in a > > different > > way? What would be your choice for ease of use? > > > > Thanx, > > > > Regards. > > > > On Thursday 05 January 2006 14:34, Chris Hall tried to type something > > > > like: > > > assuming certain relationships between projects/contacts/companies > > > > > > projects (hm) <--> (bt) contacts (bt) <--> (hm) companies > > > > > > controller: > > > > > > @project.find(1) > > > @project_contacts = @project.contacts.collect { |c| [ c.company.name + > > > " > > > > - > > > > > " + c.first_name + " " + c.last_name, c.id ] } > > > # @project_contacts => [ [ "Acme -Bob Smith", 1], ["MegaCorp - Jane > > > > Doe", > > > > > 2], ... ] > > > > > > view: > > > > > > <%= select("project", "contact_id", @project_contacts ) %> > > > > > > On 1/5/06, Gerard <mailing@gp-net.nl> wrote: > > > > Hi all, > > > > > > > > For a dropdownlist (showing "Company - FirstName Lastname'') I want to > > > > fill an > > > > object @project_contacts with "Name" from table Companies and > > > > "Firstname" > > > > > > and > > > > "Lastname" from table contacts. Any idea? > > > > > > > > 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! > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > > "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!-- "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!
Hi, We have experienced some problems with ActiveRecord::Base class of Ruby-Rails. We are building a web-application based on the Ruby-rails framework and the web-application needs to access to difference databases, so we do not pre-define our database accesses in the database.yml file. In fact, we are using the ActiveRecord::Base.establish_connection() to connect to our database, the function works perfectly. However, while we try to disconnect our database connection with ActiveRecord::Base.remove_connection(), we find out that the connection is not closed (the function seems not to be working perfectly) and we are still connected to the database and able to access to the tables. Can you please tell us, is there something we did wrong? or it is a known problem? and what should we do to disconnect a database connection ? The using version ruby-rails are : Rails 0.14.2 and ruby 1.8.3 (2005-09-21) [x86_64-linux] We are running on Linux KDE 3.4, and our database engine is postgresql The following is a code example: #################################################################################### class GenTableAs < ActiveRecord::Base end class GentestController < ApplicationController def index GenTableAs.establish_connection(:adapter => "postgresql", :host => "1.2.3.4", :port => 5432, :database => "gendb", :username => "dbuser", :password => "dbuser") @connected1 = GenTableAs.connection() @removedConnection = GenTableAs.remove_connection() @connected2 = GenTableAs.connection() # should be nil here obj1 = GenTableAs.new obj1["colaa"] = 100 obj1["colab"] = "100" obj1.save # should have raised exception or error here end end #################################################################################### the output of index @connected1: #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930 @runtime=0.0189187526702881, @connection=#<PGconn:0x2aaaac138980>, @logger=#<Logger:0x2aaaac2e7fd8 @default_formatter=#<Logger::Formatter:0x2aaaac2e7dd0 @datetime_format=nil>, @progname=nil, @logdev=#<Logger::LogDevice:0x2aaaac2e7ce0 @shift_size=1048576, @shift_age=0, @filename="./log/development.log", @mutex=#<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90 @mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0, @mon_owner=nil>, @dev=#<File:./log/development.log>>, @level=0, @formatter=nil>> removed connection: :password: dbuser :database: gendb :host: 1.2.3.4 :port: 5432 :adapter: postgresql :username: dbuser @connected2: #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930 @runtime=0.0189187526702881, @connection=#<PGconn:0x2aaaac138980>, @logger=#<Logger:0x2aaaac2e7fd8 @default_formatter=#<Logger::Formatter:0x2aaaac2e7dd0 @datetime_format=nil>, @progname=nil, @logdev=#<Logger::LogDevice:0x2aaaac2e7ce0 @shift_size=1048576, @shift_age=0, @filename="./log/development.log", @mutex=#<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90 @mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0, @mon_owner=nil>, @dev=#<File:./log/development.log>>, @level=0, @formatter=nil>> Thanks you very much !!!! Saiho __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Saiho Yuen
2006-Jan-16 16:20 UTC
[Rails] Database connection-disconnection, when, where and how????
Hi, I wish to know where and when exactly the rails connects to a database when we create a subclass of ActiveReocrd::base ? And How ActiveRecord::base work exactly? I took a look on the classes ConnectionSpecification, PostgreSQLAdapter(we are using Postgresql) and AbstractAdapter. I cannot figure out function calling stack. Can someone can tell me how the all these functions is called? As I understand the connections is stored in the @@defined_connections od the class ConnectionSpecification. And when we "remove" the connection, the function "remove connection" only remove the connection from the variable @@defined_connections, but it does not "disconnect" the connection between rails and the database, So I wonder when the rails connects to the database, and when rails decides to disconnect, and how it is done? Thanks you very much Saiho __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com