我正在尝试让它工作。我进行了适当的关联,但仍然失败。看看我的代码。
post.rb
class Post < ActiveRecord::Base
belongs_to :user
attr_accessible :content
before_create :format_content
validates :content, presence:true, length: {minimum:21}
def format_content
profil = self.user.profile
if profil.gender == "Mężczyzna"
"Wiadomość od spottera: #{self.content}"
elsif profil.gender == "Kobieta"
"Wiadomość od spotterki: #{self.content}"
end
end
end
post_spec.rb
describe "Properly formats content" do
let(:user) {FactoryGirl.create(:user)}
let!(:poscik) {FactoryGirl.create(:post) }
before(:each) {user.create_profile!(gender: "Kobieta", email: "donatella@dostojnie.pl")}
rspec_failures
Post creation valid should have content Failure/Error: poscik = user.posts.create(content: "Weird #{"a"*25}") NoMethodError: undefined method `gender' for nil:NilClass
如何正确访问模型中的其他类以及为什么找不到我的方法?我了解错误消息 - 它说我的配置文件类未定义
用户工厂
FactoryGirl.define do
factory :user do
sequence(:email) {|i|"maestro#{i}@dot.pl"}
password "kravmaga1290"
association :profile, factory: :profile, strategy: :build
end
end