Works great! Thank you Jeremy! czwartek, 26 sierpnia 2021 o 22:23:14 UTC+2 Jeremy Evans napisaĆ(a):
> On Thu, Aug 26, 2021 at 1:05 PM Mateusz Urbanski < > [email protected]> wrote: > >> Hello, I have the following Sequel query: >> >> * movies = Movie* >> * .select(Sequel[:movies][:id], Sequel.function(:avg, >> Sequel[:ratings][:value]))* >> * .left_outer_join(:ratings, movie_id: :id)* >> * .group_by(Sequel[:movies][:id])* >> * .all* >> >> The query works as expected but I have two problems: >> >> 1. I can't access the average movie rating with dot notation: >> >> *results[2].avg* >> >> *NoMethodError: undefined method `avg' for #<Movie >> @values={:id=>"d1bfc20f-45e0-4b8a-bb5b-d97b920bd408", :avg=>0.35e1}>* >> >> *from (pry):26:in `__pry__'* >> >> *results[2][:avg] *- works as expected >> > This is expected. Non-columns need to be accessed with #[]. If you want > to add an accessor method for avg: > > class Movie > def_column_accessor :avg > end > > Personally, I recommend using [:avg] instead of .avg for returned values > that are not columns in the model's dataset. > > 2. How can I get all movie columns at once, *Sequel[:movies][:*] * does >> not work. >> > > I'm guessing you want: > > Movie.select_all(:movies) > > You can use select_append to append to that selection: > > Movie. > select_all(:movies). > select_append{avg(ratings[:value])}. > left_outer_join(:ratings, movie_id: :id). > group_by{movies[:id]}. > all > > SELECT movies.*, avg(ratings.value) > FROM movies > LEFT OUTER JOIN ratings ON (ratings.movie_id = movies.id) > GROUP BY movies.id > > Thanks, > Jeremy > -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/34b21184-ab90-4658-a608-77d5fa4420e2n%40googlegroups.com.
