Search Results

Search found 2 results on 1 pages for 'dwhite'.

Page 1/1 | 1 

  • Rails ActiveRecord - Best way to perform an include?

    - by dwhite
    I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through => :collections end class User < ActiveRecord::Base has_many :collections has_many :books, :through => :collections end class Collection < ActiveRecord::Base belongs_to :book belongs_to :user end I'm trying to display a list of the books and have a link to either add or remove from the user's collection. I can't quite figure out the best syntax to do this. For example, if I do the following: Controller class BooksController < ApplicationController def index @books = Book.all end end View ... <% if book.users.include?(current_user) %> ... or obviously the inverse... ... <% if current_user.books.include?(book) %> ... Then queries are sent for each book to check on that include? which is wasteful. I was thinking of adding the users or collections to the :include on the Book.all, but I'm not sure this is the best way. Effectively all I need is the book object and just a boolean column of whether or not the current user has the book in their collection, but I'm not sure how to forumlate the query in order to do that. Thanks in advance for your help. -Damien

    Read the article

  • Passing parameters to custom RESTful routes in Rails (using :collection)

    - by dwhite
    I am trying to add a custom route to my RESTful routes using the :collection param on map.resources like so: map.resources :products, :collection => { :tagged => :get } The tagged action takes in a :tag parameter. I am able to link to the URL route using: tagged_products_path(:tag => tag.name). My issue with this is that the URL that this generates: /products/tagged?tag=electronic I would like the tag to be in the URL and not the tag, like so: /products/tagged/electronic Of course this can be accomplished by a separate named route, but I'm wondering if I'm missing something and there is a way to do this with the :collection hash. Thanks in advance for your help -Damien

    Read the article

1