Find solution

How to search in rails from two models?

Here are 29 best answers to ‘How to search in rails from two models?’ - the most relevant comments and solutions are submitted by users of Stack Overflow, Quora and Ask.Metafilter.Com.

Best solution

  • Fastest way to search two models connected via join table in rails given large data set

    I have a user model and a cd model connected through a join table 'cds_users'. I'm trying to return a hash of users plus each cd they have in common with the original user. @user.users_with_similar_cds(1,4,5) # => {:bob => [4], :tim => [1,5]} Is there a better/faster way of doing this without looping so much? Maybe a more direct way? def users_with_similar_cds(*args) similar_users = {} Cd.find(:all, :conditions => ["cds.id IN (?)", args]).each do |cd| cd.users.find(:all...

    Answer:

    You could use find_by_sql on the Users model, and Active Record will dynamically add methods for any...

    Mark as irrelevant Undo

Other solutions