我有一個characters
在following
的集合。每個character
都有一個獨特的句柄。我檢查了數據庫,以確保只有一個character
與處理barry1
。我試圖做的事:Rails不明白.include?行爲
following.include?(other_character) # false
它返回假的,但我敢肯定,barry1
是following
。它認爲id
是不同的,但只有一個barry1
。這是怎麼回事?
character.rb
has_many :following, through: :active_follow_relationships, source: :followed
看跌期權:
puts following.first.handle # barry1
puts other_character.handle # barry1
puts following.first # #<Character:0x007fef15231490>
puts other_character # #<Character:0x007fef09bb4d58>
puts following.first.id # 21
puts other_character.id # 8
你有它在你的解決方案如下[21]是不是other_character [8] – MZaragoza
如果對象不相同,則包含將不起作用。如果你想通過處理屬性檢查包含,請執行:following.any? {| f | f.handle == other_character.handle} –