I have a couple of problems which are kinda related, so I’ll out them
both here. I’ve been trying out rspec for the first time and I’m new
to has_many :through relationships, so excuse me.
I want to relate a User to a WorldCity through a Locations
relationship.
############file:models/user.rb
class User < ActiveRecord::Base
has_many :locations
has_many :world_cities, :through => :locations
end
############file:models/world_city.rb
class WorldCity < ActiveRecord::Base
has_many :locations
has_many :users, :through => :locations
end
############schema.rb
ActiveRecord::Schema.define(:version => 3) do
create_table “locations”, :force => true do |t|
t.integer “user_id”
t.integer “world_city_id”
t.datetime “created_at”
t.datetime “updated_at”
end
create_table “users”, :force => true do |t|
t.string “username”
t.string “name”
t.integer “location_id”
t.datetime “created_at”
t.datetime “updated_at”
end
create_table “world_cities”, :force => true do |t|
t.string “name”
t.float “latitude”
t.float “longitude”
t.string “country”
t.string “region”
t.integer “location_id”
t.datetime “created_at”
t.datetime “updated_at”
end
end
###########user_spec.rb
describe User do
fixtures :world_cities
before(:each) do
@user = User.create( :name => “Bob Harris”, :username =>
“bobharris” )
end
it “has a user with username ‘bobharris’” do
User.find_by_username(“bobharris”).should be_valid
end
it “has a user with username ‘bobharris with a related WorldCity’”
do
#########???
end
end
Question
I want the model to be able to assign a random WorldCity to a new user
(not really important why here). Where should this be done and how do
I test it in rspec? I’m sure if I had an example I could understand
this, but can I change the user.rb to override the create method and
add a location; does the rails framework let me assign a WordCity to a
User and take care of the relationship (that would be nice).
I’ve tried everything I can think of but can’t get my head around it.
Please help.