RoR: model on basis of a view on a LEFT JOIN?

Discussion in 'Ruby' started by harm.hoekstra@gmail.com, Nov 26, 2006.

  1. Guest

    Hi,

    I want to make a RoR webapp and on one screen the user should see
    products and his/her own rating:

    PRODUCT YOUR RATING
    product100 <no rating yet, add rating widget>
    product101 ****
    product102 **
    product103 <no rating yet, add rating widget>
    ...

    So I have a listing over products, but I need to join in the ratings of
    the current user (not of other users).

    I think there are 2 approaches:

    A. Add a getter method to the model to get the rating (filtered on
    current user)
    B. Make a view from SELECT * from products LEFT JOIN ratings ON
    products.id = ratings.product_id AND
    ratings.user_id = {user.id}

    I guess method A will result in 20 queries being fired when a list of
    20 are shown. Not that the webapp will have heavy traffic, but its a
    more clean approach if the database does the heavy lifting in one
    swoop, I think.

    So I incline to B, but are there any pitalls when making a activerecord
    model on basis of a VIEW ?

    Or perhaps even better solutions are already thought out in Rails.

    Thanks in advance,

    Harm
     
    , Nov 26, 2006
    #1
    1. Advertising

  2. On Nov 26, 2006, at 12:50 PM, wrote:

    > Hi,
    >
    > I want to make a RoR webapp and on one screen the user should see
    > products and his/her own rating:
    >
    > PRODUCT YOUR RATING
    > product100 <no rating yet, add rating widget>
    > product101 ****
    > product102 **
    > product103 <no rating yet, add rating widget>
    > ...
    >
    > So I have a listing over products, but I need to join in the
    > ratings of
    > the current user (not of other users).
    >
    > I think there are 2 approaches:
    >
    > A. Add a getter method to the model to get the rating (filtered on
    > current user)
    > B. Make a view from SELECT * from products LEFT JOIN ratings ON
    > products.id = ratings.product_id AND
    > ratings.user_id = {user.id}
    >
    > I guess method A will result in 20 queries being fired when a list of
    > 20 are shown. Not that the webapp will have heavy traffic, but its a
    > more clean approach if the database does the heavy lifting in one
    > swoop, I think.
    >
    > So I incline to B, but are there any pitalls when making a
    > activerecord
    > model on basis of a VIEW ?
    >
    > Or perhaps even better solutions are already thought out in Rails.
    >
    > Thanks in advance,
    >
    > Harm
    >


    Assuming you have a model called Product, and a model called Rating
    that belongs_to Product (and Product has_many Ratings).

    When you lookup the products with Product.find(id) or whatever, just
    use the :include => :Rating to make rails do the join. You can then
    in the conditions use the tables you are joining on and it won't blow
    up. So you should be able to end up with something like:

    Product.find:)all, :conditions => ["Ratings.user_id = ?",
    user.id] , :include => :Rating)

    I'd have to make sure that worked, but I'm relatively sure that would
    work, although I seem to remember about running into caching issues
    with it. Do a forced reload if you have missing data. To do that,
    you have to do: a_product.ratings(1) the 1 passed in forces it to
    rehit the database.
     
    Christopher Schneider, Nov 26, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    913
    Alan Silver
    Jun 5, 2006
  2. Replies:
    14
    Views:
    558
    bijeshn
    Apr 8, 2008
  3. Parthiv Joshi
    Replies:
    1
    Views:
    811
    Samuel L Matzen
    Jul 6, 2004
  4. Joachim Reichel
    Replies:
    4
    Views:
    99
    Joachim Reichel
    Jun 9, 2007
  5. Adam
    Replies:
    1
    Views:
    135
    Robert Dober
    May 21, 2008
Loading...

Share This Page