How to update table (from another)?
Have a next problem:
I have two databases: my
and not my
(call it "hammas"
). So in my hammas
i have 1 table called managers with email
and org_unit_id
fields. And in my
database i have field called users with email
and unit_id
.
Everything's clear? I'm glad. SO , next move.
I have this code:
user_emails = User.find(:all).collect {|r| r.email }
hammas_managers.each do |email, org_unit_id|
if !user_emails.include?(email)
User.create(:email => email, :user_group_id => organisational_unit_id)
end
end
SO from hammas i get emails and organisational_unit_id and pasting into my table.
EVERYHING'S WORKS. SUPERB!
But I need: If in "hammas"
some admin deleted (accidentally or specially) row, so my database still will be keeping this row (and i must delete it manually). How to I make chek automatically:
if row is deleted
blah blah blah code
Use开发者_如何学运维r.destroy(:id => ... ) or how?
Thank you!
Add this to users
has_one :hammas_manager, :foreign_key => :email, :local_key => :email, dependant => destroy
and this to hammas_manager
belongs_to :user, :foreign_key => :email, :local_key => :email, dependant => destroy
Now your application should automatically delete users when it removes the hammas_manager.
In the case you might want to 'clean up' previously orphaned users (ie. no h.m) you could run this in console (having defined the relationship as above):
User.all.each{|u| u.destroy if u.hammas_manager.blank?}
This is untested so don't run it on production data.
精彩评论