issues with validates_associated not throwing error

Hello all

I'm having an issue with ruby on rails, and it not throwing an error where it should.

I have a class 'clientpool' that is: class Clientpool < ActiveRecord::Base

set_table_name "clientpool" set_primary_key "id"

belongs_to :clients, :foreign_key => "cliname"

validates_presence_of :cliname validates_associated :client end

However, when I try to make a new clientpool, that has an invalid cliname, that is not listed in clients, it will still create it and not error out.

I see in the output log it does run the SQL to check, and the SQL querry returns 0 rows, so it should error out, but it doesn't:   SQL (0.000088) BEGIN   Client Load (0.000115) SELECT * FROM clients WHERE (clients.id = 444) LIMIT 1   SQL (0.000279) INSERT INTO clientpool (`cliname`, `prefix`, `did`) VALUES(444, 0, 0)   SQL (0.000072) COMMIT

Anyone got any ideas?

Thanks!

I'm having an issue with ruby on rails, and it not throwing an error where it should.

[SNIP]

However, when I try to make a new clientpool, that has an invalid cliname, that is not listed in clients, it will still create it and not error out.

I think you've misunderstood what the various validations do.

validates_presence_of :cliname

All this does is make sure the cliname attribute is present on the clientpool object, ie is not blank

validates_associated :client

This ensures that the associated client object is valid, ie is passing its own validations.

And another thing:

belongs_to :clients, :foreign_key => "cliname"

You probably want belongs_to :client, not :clients, since the model can only belong to a single client.

Jakob Skjerning wrote:

I think you've misunderstood what the various validations do.

> validates_presence_of :cliname

All this does is make sure the cliname attribute is present on the clientpool object, ie is not blank

> validates_associated :client

This ensures that the associated client object is valid, ie is passing its own validations.

And another thing:

> belongs_to :clients, :foreign_key => "cliname"

You probably want belongs_to :client, not :clients, since the model can only belong to a single client.

I appreciate your changes, I did make them, it still does the same SQL, but still is allowing invalid :cliname to be created that don't link to

How am I to alter how it validates a :client so that it will ensure only valid ones are there?

class Clientpool < ActiveRecord::Base

set_table_name "clientpool" set_primary_key "id"

belongs_to :client, :foreign_key => "cliname"

validates_associated :client, :on => :create validates_presence_of :cliname

end