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```
If you have an instance of
Picture@picture, you can use thebelongs_toto get it’s associatedProduct(assuming it thePictureis in fact belonging to an instance ofProductand notEmployee)