Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Relationship model and subqueries

edited August 2013 in Framework

I've been exploring has_many, belongs_to, many_many, etc. relations and it's very slick and easy to use once you get it set up.

Here's my question. I have a products table and a reviews table in addition to the elefant_user table. From the product page, I want to be able to show all reviews and the name of the reviewer, but reviewer name comes from elefant_user, not reviews.

I have defined products -> has_many reviews and elefant_user -> has_many reviews. On the products page, while looping through the reviews, what's the best way to get the reviewer's name?

Pre-elefant I would have used a subquery like SELECT reviews.*, (SELECT userdata FROM elefant_user WHERE reviews.user_id = elefant_user.id) AS userdata ...

Comments

  • edited August 2013

    Another question: is it possible to specify limitations in the $fields array for the relationship or when getting the results? (ie where approved=true)

  • If you want to fetch the elefant_user.name field along with the reviews data, you're probably better off writing a custom method to your model, since the relations are fairly simplistic and don't support that. I'm afraid they don't support additional clauses for things like where approved=true yet either...

  • OK, thanks and no problem.

Sign In or Register to comment.