Jakxna360
Jakxna360

Reputation: 905

Uninitialized constant in model affecting controller

I am working on a test application in which someone who favors something becomes a subscriber of that person.

The controller:

  def favorite_subscribe
    @favorite_subscription = FavoriteSubscription.add_favorite_subscription(current_user, @user)
    @user.favorite_subscriber_total = @user.favorite_subscriber_total + 1
    @user.save
    redirect_to :back, :notice => "Congratulations, you have favorited #{@user.username}."
  end

The model:

def self.add_favorite_subscription(user, favorite_subscribe)
  user.favorite_subscriptions.where(:subscribe_id => subscribe.id).first_or_create
end

  # Remove the favorite of the user with the other user
  def self.remove_favorite_subscription(user, favorite_subscribe)
    user.favorite_subscriptions.where(:subscribe_id => subscribe.id).destroy_all
  end

  # Get the user with the subscription
  def favorite_subscribe
    User.find(subscribe_id)
  end

I get an error that it cannot autoload constant, and that it expects my model to define it. If anybody can help that would be very appreciated.

Error is here, sorry about that:

Unable to autoload constant FavoriteSubscription, expected /home/jakxna360/rails/test/app/models/favorite_subscription.rb to define it 

Upvotes: 0

Views: 244

Answers (1)

spickermann
spickermann

Reputation: 107107

This usually means that Rails is unable to find the file in which some class is defined and therefore autoloading fails. Rails is very strict about its conventions.

In the context of the posted code, I suggest double checking that

  1. the class FavoriteSubscription (singular) is defined in a file named app/models/favorite_subscription.rb (singular), that a database table is named favorite_subscriptions (plural) and that is exists.
  2. the FavoriteSubscriptionsController (plural) is defined in a file named app/controllers/favorite_subscriptions_controller.rb (plural).

Upvotes: 1

Related Questions