Replies: 2 comments 9 replies
-
You don't want to use def self.from_hsh(hsh)
track = load(title: hsh[:title])
album = track.associations[:album] = Album.load(id: hsh[:album_id], name: hsh[:album_name])
album.associations[:artist] = Artist.load(id: hsh[:artist_id], name: hsh[:artist_name])
track
end |
Beta Was this translation helpful? Give feedback.
-
Instead of querying the dataset for hashes and creating models, can you back the model with a view? I have not had any trouble using view names instead of table names for models. Then it's something like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've recently had to map a materialized view to a model and was wondering, if there's a better alternative. Here's what I came up with:
The view:
The
search_str
is the reason I'm doing the view, but it has no relevance for the question at hand.Fetching the rows is then done via
tracks = DB[:track_search].map { |hsh| Track.from_hsh(hsh) }
, since I would like work with full models in the view. The alternative intializerTrack.from_hsh
is defined as such:Especially the part about unrestricting/restricting the primary key feels clunky. Does anyone know a cleaner solution that allows initializing models from materialized view hashes?
Beta Was this translation helpful? Give feedback.
All reactions