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:

Details
<%= text_area 'property', 'description' %>

Price
<%= text_field 'property', 'price' %>

Rcpts
<%= text_field 'property', 'receptionCount' %>

Beds
<%= text_field 'property', 'bedroomCount' %>

Baths
<%= text_field 'property', 'bathroomCount' %>

Since
<%= datetime_select 'property', 'dateRegistered' %>

Number
<%= text_field 'address', 'number' %>

Street Name 1
<%= text_field 'address', 'streetName1' %>

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 H.

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

Pat M. 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?

On 2/8/06, Marty H. [email protected] wrote:

[email protected]
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

Pat M. wrote:

On 2/8/06, Marty H. [email protected] wrote:

[email protected]
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

Street Name 1
<%= text_field 'address', 'id' %>

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

On 2/8/06, Marty H. [email protected] wrote:

the params in the debug log:

Couldn’t find Address without an ID

How would it be in the request unless it gets passed? Just do
<%= hidden_field ‘address’, ‘id’ %> and you should be good to go.