Multiple models together in one collection

43 views
Skip to first unread message

tonym

unread,
Jan 15, 2013, 4:37:04 AM1/15/13
to hobo...@googlegroups.com
Hi All,

OK, this is something I'm about to tackle, but before I start I get a feeling I need help ;)
Here's the scenario...

I have 3 models.  'Category' has a many to many relationship with the two other models (venue and event).
I need to create a single collection (output as cards) which lists all venues and events, but also, each venue or event will be listed for each category it is in (so can be listed more than once).
i.e.
Event 1 (Category 1)
Venue 1 (Category 1)
Venue 1 (Category 3)
Venue 2 (Category 1)
Venue 3 (Category 2)
Event 2 (Category 1)
Venue 3 (Category 3)
Event 2 (Category 2)

The whole list will be sorted and filtered on various fields such as date and local area that they are in (as well as category).

Is it possible to 'include' all the events and venues in my Category controller, sort them and then output the whole thing as a collection?

To be honest, not entirely sure on the best way to approach this, so any pointers would be very welcome.

And just to complicate things! I also need to do an 'outer join' on the venue to event relationships - is there an hobo way to do an out join?

Cheers, Anthony.


herrw...@gmail.com

unread,
Jan 15, 2013, 3:38:33 PM1/15/13
to hobo...@googlegroups.com
Hi Anthony,
I'm also pretty new to Rails/Hobo, but you may want to check out find_by_sql.  Given that you'll be losing database portability by hard-coding the SQL, it's a last resort, but will let you do what you want.

Category.find_by_sql ("select ... from categories, venues, events...sort by ....", bindings)

Again, I'm new, but I think you'd put in your controller:
@json=Category.find_by_sql ("....")
Then make reference in your cards to the @json variable.

Hopefully this is a start until a more complete/better answer arrives

Best regards,
Art.

Ignacio Huerta

unread,
Jan 16, 2013, 2:29:06 AM1/16/13
to hobo...@googlegroups.com, herrw...@gmail.com
Hi,

Searching with SQL is a good solution to make complex queries. Take into
account that with Rails 3 you can make a lot of queries without too much
SQL. For example:

Client.joins(:invoices => :invoice_lines).
group("clients.id").
having("sum(invoice_lines.price) > ?", 3000).
where("strftime('%Y', invoices.invoice_date) = ?", "2012")
}

Apart from this style thing, it is hard for me to imagine a case where
you need to mix two models in the same list. Maybe you could rethink
your view in a simpler way?

If you want to filter with fields from a related table, you can try the
Ransack gem. There's a Hobo gem that might help too:
https://github.com/suyccom/hobo-metasearch

Regards,
Ignacio

El 15/01/13 21:38, herrw...@gmail.com escribi�:
> --
> You received this message because you are subscribed to the Google
> Groups "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/JF5A8iFx4fEJ.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to
> hobousers+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/hobousers?hl=en.

--
Ignacio Huerta Arteche
http://www.ihuerta.net
Tel�fono: 0034 645 70 77 35
Email realizado con software libre

Bryan Larsen

unread,
Jan 16, 2013, 6:59:02 AM1/16/13
to hobo...@googlegroups.com
Probably the easiest answer is to just do things in Ruby-land. In
other words, treat the results of the query as arrays and use array
operations (like sort and +) on them.

@all = @foo.events + @foo.venues
@all.sort {|a,b| a.created_at <=> b.created_at}

This is going to be slow if the arrays are big enough that you need
pagination. But if large result sets are a rare occurrence, doing
things in ruby-land is sometimes your best option.

Bryan
> --
> You received this message because you are subscribed to the Google Groups
> "Hobo Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/hobousers/-/UmArnKDSpeEJ.

tonym

unread,
Feb 4, 2013, 12:16:51 PM2/4/13
to hobo...@googlegroups.com
Hi All,
Thought I'd close this off with how I did it...
It was actually much easier than I'd expected.  Simply created another model with has_many relationships to the models I need to mash.  It was then easy to pull that collection and grab from both models in the orders I needed.  The model is basically an index of 2 different models, and maybe I could have used indexing, but it allowed me to build a tailored collection of mashed cards with easy querying.  Also used this model to build totals for querying and quick looks ups of categories they belonged to (not ideal, as it kinda duplicates).

Regards, Anthony.
Reply all
Reply to author
Forward
0 new messages