ได้รับข้อผิดพลาดเมื่อฉันพยายามแสดงโพสต์ของผู้ใช้ที่ฉันติดตาม

ในแอปของฉัน ฉันมีเพจที่โพสต์สถานะของผู้ใช้ที่ฉันติดตาม ฉันยังใหม่กับ Rails และข้อผิดพลาดนี้กำลังฆ่าฉัน ฉันสามารถให้สิ่งอื่นได้หากต้องการ

page_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

ไทม์ไลน์.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

และนี่คือการติดตามข้อผิดพลาด

เริ่มต้น GET "/" สำหรับ 127.0.0.1 ที่ 24-08-2014 14:11:28 -0700 การประมวลผลโดย PagesController#home เป็นโหลดผู้ใช้ HTML (3.0ms) SELECT "users".* จาก "users" WHERE "users" "รหัส" = ? LIMIT 1 [["id", 4]] เสร็จสิ้นข้อผิดพลาดเซิร์ฟเวอร์ภายใน 500 ข้อใน 257ms

NoMethodError (วิธีการที่ไม่ได้กำหนด `โดยที่' สำหรับไทม์ไลน์: คลาส):

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'

แสดงผล C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.0ms) แสดงผล C:/Ruby200/lib/ruby /gems/2.0.0/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.0ms) แสดงผล 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) แสดงผล C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.1.2 /lib/action_dispatch/middleware/templates/rescues/diagnostics.erb ภายในการช่วยเหลือ/เค้าโครง (646.0ms)


person teddybear    schedule 24.08.2014    source แหล่งที่มา
comment
ดูเหมือนว่าคลาส Timeline ของคุณจะไม่ได้รับการสืบทอดจาก ActiveRecord::Base ดังนั้นจึงไม่มีเมธอด .where เดาว่าคุณต้องการทำ Post.where(...)   -  person Thomas Klemm    schedule 25.08.2014
comment
เยี่ยมมาก นั่นช่วยได้ นอกจากนี้ ถ้าคุณไม่รังเกียจ คุณช่วยบอกฉันได้ไหมว่าคุณมาได้อย่างไร? ฉันจะขอบคุณมันจริงๆ   -  person teddybear    schedule 25.08.2014
comment
@ThomasKlemm ถูกต้อง เหตุผลเดียวที่คุณไม่มี where ในโมเดลของคุณก็เพราะมันไม่ได้สืบทอดมาจาก ActiveRecord::Base   -  person Pramod Solanky    schedule 25.08.2014


คำตอบ (2)


คุณสามารถใช้ขอบเขตกับพารามิเตอร์ http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments

person cristi_razvi    schedule 05.06.2015

person    schedule
comment
มีวิธีที่ self.from_users_followed_by จะเขียน ไม่ สืบทอดจาก ActiveRecord หรือไม่ - person teddybear; 26.08.2014
comment
ฉันสามารถ กำหนดขอบเขต รหัสผู้ใช้ที่ติดตามในรูปแบบผู้ใช้ของฉันหรือ รวม เข้าด้วยกันในวิธีฮับของฉันได้หรือไม่ - person teddybear; 27.08.2014