Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4-- model with no table

I have a model, view and controller for a view (Feed) that doesn't have any "local" data. That is, the data is grabbed via a web service call to a client site and rendered in a local view.

The local site needs to verify that the object to grab from the remote site is valid (by verifying that the User that owns the Feed has a valid account) and then it displays it.

I had the logic to grab the data from the remote feed in a Feed model. Then the Feed controller takes the URL param for the feed id, passes it to the model getFeed function, and displays it.

However, since there's no table, the Feed.getFeed(params[:feed_id]) call in the controller was returning an error saying there is no table Feed.

Should I delete the Feed model and just make the getFeed function a helper function? What's the proper Rails way to deal with models with no table?

like image 245
user101289 Avatar asked Apr 11 '14 23:04

user101289


1 Answers

Just because you're using Rails, it doesn't mean every model has to inherit from ActiveRecord::Base

In your situation, I'd simply do something along these lines:

# app/models/feed.rb
class Feed
  def initialize(feed_id)
    @feed_id = feed_id
  end

  def fetch
    # ... put the web-service handling here
  end

  # encapsulate the rest of your feed processing / handling logic here
end

# app/controllers/feeds_controller.rb
class FeedsController < ActionController::Base
  def show
    @feed = Feed.new(params[:feed_id])
    @feed.fetch

    # render
  end
end

Remember: it's Ruby on Rails, not just Rails. You can do a lot more than just ActiveRecord models.

like image 150
dnch Avatar answered Oct 12 '22 23:10

dnch