0

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)

3
  • 1
    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(...). Commented Aug 24, 2014 at 22:06
  • Awesome, that helped. Also, if you don't mind, could you tell me how you came to that? I'd really appreciate it. Commented Aug 24, 2014 at 22:14
  • @ThomasKlemm is right. The only reason you dont have where on your model is becuase its not inheriting from ActiveRecord::Base Commented Aug 25, 2014 at 7:06

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way for self.from_users_followed_by to be written to not inherit from ActiveRecord?
Could I possibly scope the followed users' ids in my user model or join them together in my hub method?
0

you could also use scopes with params http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.