0

只是在 RoR 周围玩耍,试图通过一些建模找到方法。请注意以下代码:

 class Picture < ActiveRecord::Base
      belongs_to :imageable, :polymorphic => true
 end

 class Employee < ActiveRecord::Base
      has_many :pictures, :as => :imageable
 end

 class Product < ActiveRecord::Base
      has_many :pictures, :as => :imageable
 end

有没有办法通过特定图片获取产品? 我正在尝试这个,但它不起作用:

picure = Picture.last
product = Product.where(picture: picure)

它失败了:

SQLite3::SQLException: no such column: products.picture: SELECT "products".*
FROM "products" WHERE "products"."picture" IS NULL LIMIT 1```
4

1 回答 1

0

如果您有 的实例Picture @picture,则可以使用belongs_to来获取它的关联Product(假设它Picture实际上属于Productand的实例Employee

product = @picture.imageable
于 2012-11-27T21:22:28.910 回答