I’m running a rspec test to make sure that two models are associated between each other with has_many and belongs_to. Here is my test below.
describe "testing for has many links" do
before do
@post = Post.new(day: "Day 1", content: "Test")
@link = Link.new(post_id: @post.id, title: "google", url: "google.com")
end
it "in the post model" do
@post.links.first.url.should == "google.com"
end
end
The test is telling me that url is an undefined method. What’s wrong with my test? Or did I just miss something basic.
The model file for Post
has_many :links
The model file for Link
belongs_to :post
On top of that, the link model has the attribute post_id
You need to save both models to validate this relationship, also, you can use shoulda gem.
The code looks like: