Hi,
I´m new in Rails framework, and i´m trying to create my first app. I
create two tables:
clients
–id
–name
–phone
keys
–id
–client_id
–value
I use migration to create the relationship on tables:
class AddRelationship < ActiveRecord::Migration
def self.up
change_table :keys do |t|
t.belongs_to :client
end
end
def self.down
change_table :keys do |t|
t.remove_belongs_to :client
end
end
end
but, when I insert a key without a client_id, this is created
successfully, but should return an error.
Did I’m doing something wrong?
Ps: sorry about my poor english
but, when I insert a key without a client_id, this is created
successfully, but should return an error.
Did I’m doing something wrong?
Rails does not by default enforce referential integrity for your models.
You can add the necessary indexes and settings to enforce this
referential integrity in your database layer, or you could put
validation in your Key model to enforce the presence of a client_id, or
you could do both.
You could also create your keys using a construct like the following to
ensure the keys are populated correctly:
client = Client.find(1)
client.keys << Key.new(blah blah blah)
Thanks Cayce…I’ll put validation on database.
On 2 jun, 17:36, Cayce B. [email protected]