I’ve two models
class Article < ActiveRecord::Base
has_one :review
end
class Review < ActiveRecord::Base
belongs_to :article
end
Now I would like to have this method in Article
class Article < ActiveRecord::Base
has_one :review
def self.has_review?
end
end
I’ve tried with .count, .size….but I’ve errors…how can I do to have the following code working
@article = Article.find(xxx)
if @article.has_revew?
....
else
...
end
The reason why I need it is becaus I will have different action in views or controller, if there is one Review or none
Regards
This just defines a method on the instance (
def self.methoddefines a class method). The method tries to loadreview. If thereviewdoes not exist, it will be nil.!!just inverts it twice, returning true if a review exists or false if the review isnil.