查詢HAS_ONE模型我有兩個型號:具有特定屬性值
class Tree < ActiveRecord::Base
has_many :leafs
has_one :latest_leaf, -> { order(created_at: :desc) }, class_name: "Leaf"
def self.with_connected_latest_leafs
end
end
class Leaf < ActiveRecord::Base
belongs_to :tree
end
class CreateTrees < ActiveRecord::Migration
def change
create_table :trees do |t|
t.timestamps
end
end
end
class CreateLeafs < ActiveRecord::Migration
def change
create_table :leafs do |t|
t.integer :tree_id
t.string :state
t.timestamps
end
end
end
我希望所有樹木的列表,以latest_leaf
具有「連接」的狀態。
我得到這個錯誤: 'PG :: UndefinedTable:錯誤:缺少表「latest_leaf」的FROM子句項' – AndrewVos
我的錯誤。 'joins'命令中的參數應該是'Leaf'模型的表名(我想它是'leafs') –
我得到相同的錯誤 – AndrewVos