Just playing around RoR, trying to find a way through some modelling. Please, mind the code below:
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
Is there a way to get a Product by its specific Picture? I'm trying this, but its not working:
picure = Picture.last
product = Product.where(picture: picure)
Its failing with:
SQLite3::SQLException: no such column: products.picture: SELECT "products".*
FROM "products" WHERE "products"."picture" IS NULL LIMIT 1```
如果你有一个实例Picture
@picture
,您可以使用belongs_to
得到它相关的Product
(假设它的Picture
实际上是属于实例Product
,而不是Employee
)
product = @picture.imageable
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.