ruby on rails - NameError - undefined local variable or method `favorites' -


i new ruby... getting name error in user.rb

nameerror - undefined local variable or method `favorites' #<user:0x007fc0e19a8720>:   activemodel (4.0.5) lib/active_model/attribute_methods.rb:439:in `method_missing'   activerecord (4.0.5) lib/active_record/attribute_methods.rb:167:in `method_missing'   app/models/user.rb:17:in `favorited'   app/views/favorites/_favorite.html.erb:2:in `_app_views_favorites__favorite_html_erb__4203159933335848505_70233146456940' 

this user.rb file:

class user < activerecord::base   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :omniauthable   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable, :confirmable   has_many :posts, dependent: :destroy   has_many :comments   has_many :votes, dependent: :destroy   has_many :users, dependent: :destroy   mount_uploader :avatar, avataruploader    def role?(base_role)     role == base_role.to_s   end    def favorited(post)     favorites.where(post_id: post.id).first   end  end 

this favorite.rb file:

class favorite < activerecord::base

has_many :users, dependent: :destroy has_many :posts, dependent: :destroy 

end

i don't understand why getting name error.

your error reads:

nameerror - undefined local variable or method `favorites' #<user:0x007fc0e19a8720> 

in other words, it's telling you're calling method favorites on user instance, , method isn't defined object type.

that makes sense given user.rb file, has neither user-favorites association (which automatically create method you), nor explicit def favorites method. avoid error, need 1 or other.

i agree vimsha's suggestion -- looks association, you're better off using rails's built in association methods (probably has_many :favorites) writing methods association yourself.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -