Michael
Michael

Reputation: 1507

How to add models in Ruby on Rails?

I have some kind of problem when adding models to my Ruby on Rails project. I have a cute cople of controller and model, but the controller cannot access the model because the symbol seems to be... undefined.

So it seems I don't understand AT ALL how Rails chooses which classes to load and which not to load.

Here's my model file (and YES, I know, I should have a file for each model. And I did. But I've been trying everything to make it work, and currently I just have one) :

class Feed
  include Mongoid::Document
  field       :name,        :type => String
  field       :description, :type => String
  field       :public,      :type => Boolean, :default => false
  has_many    :posts, :class_name => 'FeedPost'
  embedded_in :user

  def isSubscribed? user
    res = user.feedsubscribtions.where :userId => self.user.id, :feedId => self.id
    res.count > 0
  end
end

class Feedsubscribtion
  include Mongoid::Document
  field       :userId, :type => String
  field       :feedId, :type => String
  embedded_in :user

  def Owner
    user = User.where :id => self.userId
    if user != nil then user.first else nil end
  end

  def Feed
    user = Owner()
    feed = user.feeds.where :id => self.feedId
    if feed != nil then feed.first else nil end
  end
end

Everything with the Feed model and controllers work just swell. I just have this problem with FeedControllers and a few other classes, that doesn't have associated controllers, and just don't seem to even exist in my Rails project as far as the controllers are concerned.

And then I have my feedsubscribtion_controller :

class FeedsubscribtionsController < ApplicationController
  has_to_be_connected

  def create
    if session[:user_id] != params[:id]
      @self        = CurrentUser()
      subscribtion = Feedsubscribtion.new :userId => params[:id], :feedId => params[:feed]
      @self.feedsubscribtions |=  [ subscribtion ]
      render json: { success: @self.save(validate: false), feed: params[:feed] }
    end
  end

  def destroy
    if session[:user_id] != params[:id]
      @self    = CurrentUser()
      uid, fid = BSON::ObjectId.new(params[:id]), BSON::ObjectId.new(params[:feed])
      @feed    = @self.feedsubscribtions.where :userId => uid, :fid => fid
      if @feed.count > 0
        @feed.first.delete
    @self.save(validate: false)
      end
    end
  end
end

I've been trying to make a workaround, where a method of Feed would return me the instance of Feedsubscribtion. It worked for "create" : not for "destroy". Because it seems Mongoid::Document.where tries to use FeedSubscribtion. And fails, of course.

What's happening to me ? Why the hell Rails doesn't make Feedsubscribtion available outside of its own file ??

Upvotes: 1

Views: 1494

Answers (1)

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40313

First, rename your model to FeedSubscription, then place it in a file at app/models/feed_subscription.rb. Then change the references at your controllers to access the FeedSubscription class and rename your methods Owner and Feed and CurrentUser, as methods should always be defined in lowercase in Ruby.

When you define something starting with an uppercase in Ruby it makes it a constant.

Upvotes: 4

Related Questions