简体   繁体   中英

Active record joins in rails 3.1

Trying to return all comments associated with articles written by a particular user. The following code gives me the error:

undefined method `joins' for #<User:0x000001042c8bc0>

under Rails 3.1

class User
  has_many :articles

  def comments
    self.joins(:articles => :comments)
  end

end

You probably want a has_many:through here.

class User
  has_many :articles
  has_many :comments, :through => :articles
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM