2
我找找到不被用戶所擁有的書籍以正確的方式......這裏有關聯:Rails的記錄不關聯
書 -
has_many :owns, :dependent => :destroy
用戶 -
has_many :owns, :dependent => :destroy
has_many :owned_books, :through => :owns, source: :book, :dependent => :destroy
自己 -
belongs_to :user
belongs_to :book
我有(例如,Book.where(title => "Foo")
或Book.where(title => "Bar")
),並希望添加一個篩選器,用戶只能看到那些不具有該標題的圖書。這對我來說很明顯,這應該是書模型中的一種新關聯,但我有點迷失。
這比我預期的更簡單,謝謝。 – user3141095
這就是擁有正確數據結構的好處。像這樣的查詢有時非常簡單。 – tadman
這是什麼魔術!!!!!非常感謝,先生:D 'owned_book_ids' @@我沒有在任何其他Rails教程的任何地方看到這個幫助器? – user3448806