I created 3 models, User, City, UserCity.
User class:
class User < ActiveRecord::Base
has_many :user_cities, :dependent => :destroy, :uniq => true
has_many :cities, :through => :user_cities
end
City class:
class City < ActiveRecord::Base
has_many :user_cities, :dependent => :destroy, :uniq => true
has_many :users, :through => :user_cities
end
UserCity class:
class UserCity < ActiveRecord::Base
belongs_to :user
belongs_to :city
end
And then I tried
u = User.new()
c = City.new()
u.cities << c
c.users << u
p UserCity.all.size # => 2
user_cities table had duplicates. So then, I coded
UserCity class:
class UserCity < ActiveRecord::Base
validates :user_id, :uniqueness => {:scope => :city_id}
belongs_to :user
belongs_to :city
end
and run the same ruby code above. But it failed after c.users << u because I prohibited duplicate.
How can I make u.cities have c and c.users have u without duplicating data in join table?
Added:
So, if I choose only c.users << u, can I only do this for cities?
cities = Array.new()
UserCity.where(:user_id => u.id).each do |uc|
cities << City.find(uc.city_id)
end
Choose either
u.cities << corc.users << u. Each of them cause a row inserted in to the join table.