# Beginner - problem with understanding relationships

**URL:** <https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462>\
**Category:** Rails\
**Created:** [February 8, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462 "2006-02-08T17:40:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/1 "2006-02-08T17:40:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/2 "2006-02-08T17:43:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/3 "2006-02-08T17:46:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/4 "2006-02-08T17:52:08Z")

</div>

On 2/8/06, Marty H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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

---

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/5 "2006-02-08T20:57:36Z")

</div>

Pat M. wrote:

> On 2/8/06, Marty H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](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

---

<div class="post-metadata">

**Author:** ![djmorphine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@djmorphine](https://www.ruby-forum.com/u/djmorphine)\
**Post date:** [February 8, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/beginner-problem-with-understanding-relationships/102462/6 "2006-02-08T22:42:58Z")

</div>

On 2/8/06, Marty H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
