0
votes

In my app I have a page that posts the statuses of users I follow. I'm new to rails and this error is killing me. I can give anything else if its needed

pages_controller.rb

def home
  if signed_in?
    @hub_items = current_user.hub.paginate(page: params[:page])
  end
end

user.rb

def hub
  Timeline.from_users_followed_by(self)
end

timeline.rb

def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
                     WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
      user_id: user.id)
end

And this is the error trace.

Started GET "/" for 127.0.0.1 at 2014-08-24 14:11:28 -0700 Processing by PagesController#home as HTML User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]] Completed 500 Internal Server Error in 257ms

NoMethodError (undefined method `where' for Timeline:Class):

app/models/timeline.rb:47:in `from_users_followed_by'
app/models/user.rb:24:in `hub'
app/controllers/pages_controller.rb:5:in `home'

Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.0ms) Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms) Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (5.0ms) Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (646.0ms)

2
Seems like your Timeline class in not inheriting from ActiveRecord::Base, so the .where method is not present. Guess you're wanting to do a Post.where(...).Thomas Klemm
Awesome, that helped. Also, if you don't mind, could you tell me how you came to that? I'd really appreciate it.teddybear
@ThomasKlemm is right. The only reason you dont have where on your model is becuase its not inheriting from ActiveRecord::BasePramod Solanky

2 Answers

1
votes
class Timeline
  def self.do_something
    where(...) # is equivalent to self.where(...)
    # The `where` method should in your app be defined by ActiveRecord, 
    # and present in those classes that inherit from ActiveRecord::Base
    # (`class Post < ActiveRecord::Base; end). Since Timeline is no class
    # backed by a database table, you will want to load `Post.where(...)
    # or SomeModel.where(...) to display in your timeline.
  end
end