excid3/simple_calendar

Render data using ActiveRecord::Base.connection.execute(sql)

ademir10 opened this issue · 11 comments

Hi guys!
I'm trying to render my query result using: ActiveRecord::Base.connection.execute(sql), but I receive this message:
undefined method `start_time' for #Hash:0x00007fc5692a99d8
<%= month_calendar events: @meetings do |date, meetings| %>

this is my query:
sql = "select a.* from meetings a join(select client, max(id) as id from meetings group by client) b on (a.id = b.id)"
@meetings = ActiveRecord::Base.connection.execute(sql)

Do you know if is possible to use ActiveRecord::Base.connection.execute(sql) ?
thanks guys!

You have to pass in an array of model objects, not a hash. If you can transform your query results to objects, you'll be fine.

Hi excid3! can you send a example please?

I also tried to convert my query result this way:
@meetings = ActiveRecord::Base.connection.execute(sql).to_a
but still not working...

I'm not really sure. Never done it before.

Something like this probably: https://stackoverflow.com/questions/24097457/convert-executed-sql-result-to-a-list-of-model-object

I'm not really sure. Never done it before.

Something like this probably: https://stackoverflow.com/questions/24097457/convert-executed-sql-result-to-a-list-of-model-object

thank you, man! let's try it!

any suggestion guys? no way... I'm still making it works.
There is any way to rewrite this query but i ruby?
select a.* from meetings a join(select client, max(id) as id from meetings where status != 'COMPROU' group by client) b on (a.id = b.id)
I don't know if is possible to do this same query with ruby..

Try to just use the ActiveRecord join and group methods. This is a pretty simple query so you can almost certainly do that with AR.

Try to just use the ActiveRecord join and group methods. This is a pretty simple query so you can almost certainly do that with AR.

Could you send some example? i've never tried something like this before..

I don't have time to unfortunately, but read through this and search on StackOverflow. https://guides.rubyonrails.org/active_record_querying.html

I don't have time to unfortunately, but read through this and search on StackOverflow. https://guides.rubyonrails.org/active_record_querying.html

thank you so much for all support! finally is done!
this way it's more easy to be done:
sql = "select a.* from meetings a join(select client, max(id) as id from meetings where status != 'COMPROU' group by client) b on (a.id = b.id)"
@meetings = Meeting.find_by_sql(sql)
Just using: .find_by_sql(sql)

God bless you excid3!

Awesome, that turned out easy!