Forum: Ruby on Rails acts_as ... hourly_list?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E7cff3cfd41c495e1012227d7dc24202?d=identicon&s=25 Luis Lavena (luislavena)
on 2006-04-11 23:23
Hi,

My question is a bit of wonder and a bit of enlightment.

I have two models, Channel and ChannelItem, the second is a list of
Events, ordered by position (because I'm using acts_as_list).

Later I use a method in Channel to recalculate the hour when that show
come online, based on the duration of it and doing math based on the
previous one duration and start time, starting from 0:00.

So, with this info (channel_items):

id | channel_id | event_id | position | starttime
 1 | 1          | 3        | 1        |
 2 | 1          | 5        | 2        |
 3 | 1          | 2        | 3        |
 4 | 1          | 1        | 4        |
[...]

After processing it with recalculate_hours get this (all starting
00:00):

id | channel_id | event_id | position | starttime
 1 | 1          | 3        | 1        | 00:00 (event.duration = 1:00)
 2 | 1          | 5        | 2        | 01:00 (event.duration = 0:30)
 3 | 1          | 2        | 3        | 01:30 (event.duration = 0:45)
 4 | 1          | 1        | 4        | 02:15 (event.duration = 0:30)
[...]

Ok, Every time I rearrange the schedule (the channel information), need
to call recalculate method, which is a bit expensive to do for 24hs
schedules

How could I implement this methods as "acts" so remove the dependency on
acts_as_list and use my own? also if I could get "next available
position" as time instead of integer?

Hope the question is clear, and hope someone could enlight me in the
right direction.

Regards,

Luis
Eb96620826312516bb4a2d6deeee6688?d=identicon&s=25 Jean-Christophe Michel (Guest)
on 2006-04-13 09:49
(Received via mailing list)
Luis Lavena wrote:
> Hi,
...

>
> How could I implement this methods as "acts" so remove the dependency on
> acts_as_list and use my own? also if I could get "next available
> position" as time instead of integer?

I'd try to have the starttime column be computed If your db doesn't
handle time computations, I'd change your column type to BIGINT and
store the time offest in seconds (formatting into minutes:seconds can be
done in an helper).
Supposing you have a column duration and you use Postgres:
  select id,channel_id,duration, COALESCE((select sum(duration) from
test as test2 where test2.position<test1.position), '0:00:00') as
starttime from test as test1 order by position;
will return a computed starttime.

Then you may build a view in Postgres to make your virtual column
starttime visible by AR, or use a custom find_by_sql.

If you want to make an acts_as, see on wiki.rubyonrails.org, there's a
tutorial to write an acts_as. Base thing is to write a module that will
be evaluated in ActiveRecord and change the behaviour as you wish.
This topic is locked and can not be replied to.