I'm new to Rails3, and I'm just trying to get one last thing to work
before I call it a night. The situation is the following (please if
the code is horrible, just let me know, still learning):
I want to log a dive. I might have a new location on that dive at
which point I have to create a new Location, and then create the dive.
A dive has_one location. A location has_many dives. Currently the
foreign key is on dive as location_id.
How do I properly, in my dives_controller, generate the location, get
the ID, and pass it on to my new dive? It would be nice to have the
constructor of Location called, but if it doesn't work that way, then
that's okay too.
I'm new to Rails3, and I'm just trying to get one last thing to work
before I call it a night. The situation is the following (please if
the code is horrible, just let me know, still learning):
I want to log a dive. I might have a new location on that dive at
which point I have to create a new Location, and then create the dive.
A dive has_one location. A location has_many dives. Currently the
foreign key is on dive as location_id.
Then Dive belongs_to Location. Go review the difference between
belongs_to and has_one.
You rarely need 'initialize' when creating new model instances (and in
this case you don't).
The beauty of ActiveRecord is that it automagically gives you instance
variables for all table columns.
If you need any variables that are not a table column use
attr_accessor :foo_bar
You rarely need 'initialize' when creating new model instances (and in
this case you don't).
Good catch! But make that "never", not "rarely". ActiveRecord
constructors generally shouldn't be overridden. Instead, use before_ or
after_create callbacks to set values (as in your example).
The beauty of ActiveRecord is that it automagically gives you instance
variables for all table columns.
If you need any variables that are not a table column use
attr_accessor :foo_bar