Marty Henderson
2006-Feb-08 16:40 UTC
[Rails] beginner - problem with understanding relationships
Hi folks, I''m new to this... so hopefully someone can help me. I have a few objects... Property and Address and Landlord... where a Property has an address and a landlord has an address also. I''ve modelled this in the db with the properties table having an address_id and the landlord table having an address id. My rb looks like: class Property < ActiveRecord::Base has_one :address end class Address < ActiveRecord::Base end the controller looks like: def create @property = Property.new(params[:property]) @property.address = Address.new(params[:address]) if @property.save flash[:notice] = ''Property was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end def edit @property = Property.find(params[:id]) @address = @property.address @landlords = Landlord.find_all end the form to save: <!--[form:property]--> <p><label for="property_description">Details</label><br/> <%= text_area ''property'', ''description'' %></p> <p><label for="property_price">Price</label><br/> <%= text_field ''property'', ''price'' %></p> <p><label for="property_receptionCount">Rcpts</label><br/> <%= text_field ''property'', ''receptionCount'' %></p> <p><label for="property_bedroomCount">Beds</label><br/> <%= text_field ''property'', ''bedroomCount'' %></p> <p><label for="property_bathroomCount">Baths</label><br/> <%= text_field ''property'', ''bathroomCount'' %></p> <p><label for="property_dateRegistered">Since</label><br/> <%= datetime_select ''property'', ''dateRegistered'' %></p> <p><label for="property_address_number">Number</label><br/> <%= text_field ''address'', ''number'' %></p> <p><label for="property_address_streetName1">Street Name 1</label><br/> <%= text_field ''address'', ''streetName1'' %></p> but this is chucking out errors looking for a property_id column on the database table address. I dunno... I come from a java background and it ''feels'' a bit backward that it should be doing this. I think i''m probably missing something simple... so if anyone could even point me in the right direction that would be great! Thanks in advance. Marty Henderson -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2006-Feb-08 16:43 UTC
[Rails] beginner - problem with understanding relationships
class Property < ActiveRecord::Base belongs_to :address end instead. I had the same exact problem when I first started. Same as you, I came from a Java background and ''has_one'' initially made more sense. Doesn''t take very long to get used to it though. Pat On 2/8/06, Marty Henderson <henderson.marty@gmail.com> wrote:> Hi folks, > > I''m new to this... so hopefully someone can help me. > I have a few objects... Property and Address and Landlord... where a > Property has an address and a landlord has an address also. > > I''ve modelled this in the db with the properties table having an > address_id and the landlord table having an address id. > > My rb looks like: > > class Property < ActiveRecord::Base > has_one :address > end > > class Address < ActiveRecord::Base > end > > the controller looks like: > > def create > @property = Property.new(params[:property]) > @property.address = Address.new(params[:address]) > > if @property.save > flash[:notice] = ''Property was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > def edit > @property = Property.find(params[:id]) > @address = @property.address > @landlords = Landlord.find_all > end > > the form to save: > > <!--[form:property]--> > <p><label for="property_description">Details</label><br/> > <%= text_area ''property'', ''description'' %></p> > > <p><label for="property_price">Price</label><br/> > <%= text_field ''property'', ''price'' %></p> > > <p><label for="property_receptionCount">Rcpts</label><br/> > <%= text_field ''property'', ''receptionCount'' %></p> > > <p><label for="property_bedroomCount">Beds</label><br/> > <%= text_field ''property'', ''bedroomCount'' %></p> > > <p><label for="property_bathroomCount">Baths</label><br/> > <%= text_field ''property'', ''bathroomCount'' %></p> > > <p><label for="property_dateRegistered">Since</label><br/> > <%= datetime_select ''property'', ''dateRegistered'' %></p> > > <p><label for="property_address_number">Number</label><br/> > <%= text_field ''address'', ''number'' %></p> > > <p><label for="property_address_streetName1">Street Name 1</label><br/> > <%= text_field ''address'', ''streetName1'' %></p> > > but this is chucking out errors looking for a property_id column on the > database table address. > I dunno... I come from a java background and it ''feels'' a bit backward > that it should be doing this. > I think i''m probably missing something simple... so if anyone could even > point me in the right direction that would be great! > > Thanks in advance. > > Marty Henderson > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Marty Henderson
2006-Feb-08 16:47 UTC
[Rails] Re: beginner - problem with understanding relationships
Pat Maddox wrote:> class Property < ActiveRecord::Base > belongs_to :address > end > > instead. I had the same exact problem when I first started. Same as > you, I came from a Java background and ''has_one'' initially made more > sense. Doesn''t take very long to get used to it though. > > Patahhhhhh star man. one more for you.... when I''m doing an update then and have the code Address.find(params[:address_id]) the address_id bit is wrong... what needs to be here? -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2006-Feb-08 16:51 UTC
[Rails] Re: beginner - problem with understanding relationships
On 2/8/06, Marty Henderson <henderson.marty@gmail.com> wrote:> Pat Maddox wrote: > > class Property < ActiveRecord::Base > > belongs_to :address > > end > > > > instead. I had the same exact problem when I first started. Same as > > you, I came from a Java background and ''has_one'' initially made more > > sense. Doesn''t take very long to get used to it though. > > > > Pat > > ahhhhhh star man. > > one more for you.... when I''m doing an update then and have the code > Address.find(params[:address_id]) the address_id bit is wrong... what > needs to be here? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Depends on what the parameter is in the form. Could be params[:id] or params[:address][:id], I''m not entirely sure. Look at your form code to see where the id is being passed in. Pat
Marty Henderson
2006-Feb-08 19:57 UTC
[Rails] Re: Re: beginner - problem with understanding relationships
Pat Maddox wrote:> On 2/8/06, Marty Henderson <henderson.marty@gmail.com> wrote: >> >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > Depends on what the parameter is in the form. Could be params[:id] or > params[:address][:id], I''m not entirely sure. Look at your form code > to see where the id is being passed in. > > Patargh it works when I do this in the form <p><label for="property_address_streetName1">Street Name 1</label><br/> <%= text_field ''address'', ''id'' %></p> but when I take it out the address id doesn''t seem to be on the request? does this have to be on and hidden? the params in the debug log: Couldn''t find Address without an ID RAILS_ROOT: ./script/../config/.. Application Trace | Framework Trace | Full Trace C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:407:in `find'' #{RAILS_ROOT}/app/controllers/properties_controller.rb:41:in `update'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:407:in `find'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in `send'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in `perform_action_without_filters'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in `perform_action_without_benchmark'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `measure'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in `perform_action'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `send'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `process_without_session_management_support'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in `process'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in `dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in `handle_dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in `service'' C:/Workspace/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' C:/Workspace/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in `dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__'' C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__'' C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'' script/server:3 C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:407:in `find'' #{RAILS_ROOT}/app/controllers/properties_controller.rb:41:in `update'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in `send'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in `perform_action_without_filters'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in `perform_action_without_benchmark'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `measure'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in `perform_action'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `send'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in `process_without_session_management_support'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in `process'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in `dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in `handle_dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in `service'' C:/Workspace/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' C:/Workspace/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/Workspace/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in `dispatch'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__'' C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__'' C:/Workspace/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' C:/Workspace/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in `require'' script/server:3 Request Parameters: {"commit"=>"Edit", "property"=>{"bedroomCount"=>"2", "price"=>"900", "dateRegistered(1i)"=>"2006", "dateRegistered(2i)"=>"2", "receptionCount"=>"2", "landlord_id"=>"1", "bathroomCount"=>"2", "dateRegistered(3i)"=>"8", "dateRegistered(4i)"=>"16", "description"=>"jhgjkhfkjdhfkjdh", "dateRegistered(5i)"=>"44"}, "id"=>"6", "address"=>{"number"=>"8901", "streetName1"=>"jlkjlkjk"}} Show session dump -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2006-Feb-08 21:42 UTC
[Rails] Re: Re: beginner - problem with understanding relationships
On 2/8/06, Marty Henderson <henderson.marty@gmail.com> wrote:> Pat Maddox wrote: > > On 2/8/06, Marty Henderson <henderson.marty@gmail.com> wrote: > >> > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > > Depends on what the parameter is in the form. Could be params[:id] or > > params[:address][:id], I''m not entirely sure. Look at your form code > > to see where the id is being passed in. > > > > Pat > > argh > > it works when I do this in the form > <p><label for="property_address_streetName1">Street Name 1</label><br/> > <%= text_field ''address'', ''id'' %></p> > > but when I take it out the address id doesn''t seem to be on the request? > does this have to be on and hidden? > the params in the debug log: > > Couldn''t find Address without an IDHow would it be in the request unless it gets passed? Just do <%= hidden_field ''address'', ''id'' %> and you should be good to go.