I was hoping someone would be able to help me with creating a method. I have two tables. What I am trying to do is create a list of all the id''s from table2 that aren''t currently referenced by Table1''s address_id column. That way, when I create a new customer I can have a drop down list in the view of all the addresses that are not currently being used. ##### Database ##### Table1 id name address_id Table2 id address ##### Models ##### class Table1 < ActiveRecord::Base belongs_to :table2 end ---------------------------------------------------- class Table2 < ActiveRecord::Base has_one :table1 end ##### Controller ###### def new @table2 = Table2.find_all # This is what I want to change @table1 = Table1.new end ###### View ##### <%= collection_select ''table1, ''table2_id'', @table2, :id, :address -%> I am very new to ruby and rails and know how to do easier searches, but doing a search from two tables and matching the results is a little beyond me at this point. If there is a different or better way to go about doing this, I am open to suggestions. Any help would be greatly appreciated. Thanks, ~D -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060228/49a5f39e/attachment-0001.html
Am Dienstag, den 28.02.2006, 13:40 -0600 schrieb Dan Wright:> I was hoping someone would be able to help me with creating a method. > > I have two tables. What I am trying to do is create a list of all the > id''s from table2 that aren''t currently referenced by Table1''s > address_id column. That way, when I create a new customer I can have a > drop down list in the view of all the addresses that are not currently > being used. > > ##### Database ##### > Table1 > id > name > address_id > > Table2 > id > address > > ##### Models ##### > class Table1 < ActiveRecord::Base > belongs_to :table2 > end > ---------------------------------------------------- > class Table2 < ActiveRecord::Base > has_one :table1 > end > > ##### Controller ###### > def new > @table2 = Table2.find_all # This is what I want to change > @table1 = Table1.new > end > > ###### View ##### > <%= collection_select ''table1, ''table2_id'', @table2, :id, :address -%> > > I am very new to ruby and rails and know how to do easier searches, > but doing a search from two tables and matching the results is a > little beyond me at this point. > If there is a different or better way to go about doing this, I am > open to suggestions.I gave your classes different names, because i don''t know how Rails will deal with pluralization and digits. class Address < ActiveRecord::Base # was Table1 belongs_to Person end class Person < Activerecord::Base # was Table2 has_one :address end # Cotroller: def new @unassociated_addresses = Address.find( :all, :include => :person, :conditions => ''addresses.person_id IS NULL'' ) @person = Person.new @address = Address.new end # View: <%= collection_select ''address, ''person_id'', @unassociated_addresses, :id, :address -%> This code is completely untested. -- Norman Timmler http://blog.inlet-media.de
On Mar 1, 2006, at 3:53 AM, Norman Timmler wrote:> Am Dienstag, den 28.02.2006, 13:40 -0600 schrieb Dan Wright: > >> I was hoping someone would be able to help me with creating a method. >> >> I have two tables. What I am trying to do is create a list of all >> the >> id''s from table2 that aren''t currently referenced by Table1''s >> address_id column. That way, when I create a new customer I can >> have a >> drop down list in the view of all the addresses that are not >> currently >> being used. >> > > I gave your classes different names, because i don''t know how Rails > will > deal with pluralization and digits. >By default, it adds or removes an ''s''. I have an application right now that has a table whose name ends in a digit and it works just fine. It would be like a table of (US) tax forms W-2 called "w2s" with "class Worker < ActiveRecord::Base; has_many :w2s; end" -Rob