granite icon indicating copy to clipboard operation
granite copied to clipboard

through in has_many doesn't work

Open confact opened this issue 3 years ago • 0 comments

Hi

I try to do a has_many through relationship, like this:

class User < Granite::Base
  ....
  has_many albums : Album
  has_many :photos, class_name: Photo, through: :albums
end

and the album has:

class Album < Granite::Base
  ...
  belongs_to user : User, foreign_key: user_id : Int32?
  has_many photo : Photo
end

and photo:

class Photo < Granite::Base
  ...
  belongs_to album : Album, foreign_key: album_id : Int32?
end

But when I do this:

user.photos

I get nothing, user.albums gives me a list of the albums, and Photo.first give me the first photo, with correct album_id.

confact avatar Jan 14 '22 17:01 confact