respond_to do |format|
if @timesheet.create_command_officer(params[:command_officer])
This is working fine. But when I use the same code in
FireFighter#create:
if @timesheet.create_fire_fighter(params[:fire_fighter])
I get this error:
NoMethodError in Fire fightersController#create
undefined method `create_fire_fighter’ for
“#Timesheet:0x3f8b938”:Timesheet
As far as I know everything is set up identical to Command Officer and I
don’t know why the create_fire_fighter doesn’t work, while it does work
for Command Officer?
You do know that Timesheet.last will return an arbitrary timesheet, and
that you can’t predict which one it returns, right?
That is not entirely true. It is ‘Timesheet.first’ that would better be
renamed to ‘Timesheet.random’ Same goes for Timesheet.find_by_name
(that is really “pick an arbitrary entry with that name”).
But ‘Timesheet.last’ actually has a default ordering on ‘id DESC’.
I saw that behavior in Rails 2.3.5 and 3.0.3. Here in 3.0.3:
Timesheet Load (0.2ms) SELECT “timesheets”.* FROM “timesheets” LIMIT
1
Timesheet Load (0.2ms) SELECT “timesheets”.* FROM “timesheets” ORDER
BY timesheets.id DESC LIMIT 1
Timesheet Load (0.2ms) SELECT “timesheets”.* FROM “timesheets” WHERE
(“timesheets”.“name” = ‘Jon’) LIMIT 1
This caused us quite some fun at work (e.g. flickering tests at best,
unpredictable results at worst), so I eventually implemented a “.single”
method that barfs loudly if you expect to find only zero or 1 entries
for a certain query or in a certain array and then unexpectly, there are
more entries there …
For that reason, at work, I advise against the use of:
Still using the data model I advised you against, I see…
Yes, unfortunately my boss has been pushing to get a first version
online within two weeks. I’ve decided to use this current structure for
now and then restructure after launch.
…but in any case, I think you want @timesheet.fire_fighters.create,
not @timesheet.create_fire_fighter , which doesn’t exist on has_many.
Thanks, I did not know that ‘create_modelname’ was only available with
has_one relationships.
I tested it with the console, Tiemsheet.last.fire_fighters returns an
empty array []
You do know that Timesheet.last will return an arbitrary timesheet, and
that you can’t predict which one it returns, right?
Yes, I was just using that to test if the association was working
correctly.
Still using the data model I advised you against, I see…
Yes, unfortunately my boss has been pushing to get a first version
online within two weeks. I’ve decided to use this current structure for
now and then restructure after launch.
I advise against this course of action in the strongest possible terms.
Normalizing the schema will be easy. Refactoring the application code
will be easy if you have proper tests. But normalizing the data already
entered into the denormalized schema will be nearly impossible.
This is one of those things that you want to get right the first time.
Normalize your schema. It’s easier to denormalize later than to
normalize later.
…but in any case, I think you want @timesheet.fire_fighters.create,
not @timesheet.create_fire_fighter , which doesn’t exist on has_many.
Thanks, I did not know that ‘create_modelname’ was only available with
has_one relationships.
Did you check the docs?
I tested it with the console, Tiemsheet.last.fire_fighters returns an
empty array []
You do know that Timesheet.last will return an arbitrary timesheet, and
that you can’t predict which one it returns, right?
Yes, I was just using that to test if the association was working
correctly.
OK. You’d probably be better served to get good automated tests
working.
I advise against this course of action in the strongest possible terms.
Normalizing the schema will be easy. Refactoring the application code
will be easy if you have proper tests. But normalizing the data already
entered into the denormalized schema will be nearly impossible.
This is one of those things that you want to get right the first time.
Normalize your schema. It’s easier to denormalize later than to
normalize later.
Ok thanks, I am going to ask my boss to move the launch deadline in
order to avoid those problems.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.