Reputation: 430
I'm new to Rails. Similar code worked when I compledted Hartl's tutorial. The difference is that there was a Micropost model, and now I've created Hotel model for my own app.
And Model specs do not pass. I think that problem is about assotiations, but who knows... Spent the whole day yesterday and couldn't make them pass. I appreciate any help. Thanks!
Here is error and my code:
Failures:
1) Hotel
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xb3c2d2c>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
2) Hotel
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xb5b1200>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
3) Hotel
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xb7364b8>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
4) Hotel
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xb42a9cc>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
5) Hotel when user_id is not present
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xbc7ba40>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
6) Hotel user
Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
NoMethodError:
undefined method `hotel' for #<User:0xbefae4c>
# ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 1.11 seconds
33 examples, 6 failures
Failed examples:
rspec ./spec/models/hotel_spec.rb:10 # Hotel
rspec ./spec/models/hotel_spec.rb:12 # Hotel
rspec ./spec/models/hotel_spec.rb:11 # Hotel
rspec ./spec/models/hotel_spec.rb:15 # Hotel
rspec ./spec/models/hotel_spec.rb:19 # Hotel when user_id is not present
rspec ./spec/models/hotel_spec.rb:13 # Hotel user
hotel.rb
class Hotel < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :user_id, presence: true
end
user.rb
class User < ActiveRecord::Base
has_many :hotels
before_save { email.downcase! }
before_create :create_remember_token
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
.....
hotel_spec.rb
require 'spec_helper'
describe Hotel do
let(:user) { FactoryGirl.create(:user) }
before { @hotel = user.hotel.build(title: "Saffron") }
subject { @hotel }
it { should respond_to(:title) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
its(:user) { should eq user }
it { should be_valid }
describe "when user_id is not present" do
before { @hotel.user_id = nil }
it { should_not be_valid }
end
end
Migration for Hotels table
class CreateHotels < ActiveRecord::Migration
def change
create_table :hotels do |t|
t.string :title
t.integer :user_id
t.timestamps
end
add_index :hotels, [:user_id, :created_at]
end
end
Upvotes: 0
Views: 210
Reputation: 13181
Welcome to rails
You should read the guide on associations
This code means a a user has many hotels (with a s):
class User < ActiveRecord::Base
has_many :hotels
So if you are looking for all the hotels of a user
you can use the code user.hotels
, which is a collection (of many hotels), so you can use all ruby and rails collections methods. For example:
# This will output to your console the name of each hotels linked to that user
user.hotels.each do |hotel|
puts hotel.name
end
Now if you are looking for the first hotel of the collection you can use users.hotels.first
And a lot more things are possible. Read the guide I linked at the beginning of my message, also you can have a look at http://railscasts.com that have a lot of good material available for free (and some for a fee, but for the basic learning, the free episodes are very good)
Upvotes: 1