Doing JOINs the Rails way
-
How can I do a Rails find JOIN without SQL, using the two tables' existing has_and_belongs_to_many relationships? I have two classes Sample and User, which have: has_and_belongs_to_many :users, :uniq => true has_and_belongs_to_many :samples, :uniq => true relationships in their respective model files. This sets up the many-to-many relationship between the two classes. Let's say I want to associate a Sample instance with a User instance: Sample.find_by_id(params[:sample][:id]).users << User.find_by_id(params[:user_id]) So far so good. But I'd like to avoid duplicates of a combination of sample_id and user_id. Therefore, I'd like to search on samples_users to find a previously-entered record which matches those two id numbers. I could do this with a SQL JOIN but I'd like to learn the Rails way, so that I don't have to write custom SQL that will likely break during development. How to I use the find method on Sample to get any matches with a specified sample_id and user_id?>
-
Answer:
This kind of sidesteps your question, but avoiding duplicates can be done at the database level by creating a unique index across sample_id and user_id in the samples_users table like so: add_index :samples_users, [:sample_id, :user_id], :unique => true There's a chance you'll need a begin / rescue for saving - I'm not sure if it just returns false when things don't work out or if AR throws an exception.
Blazecock Pileon at Ask.Metafilter.Com Visit the source
Other answers
sample = Sample.find_by_id(params[:sample][:id]) sample.users << User.find_by_id(params[:user_id]) unless sample.users.find(params[:user_id])
bitdamaged
Thank you both kindly. I wish there were better tutorials for Rails.
Blazecock Pileon
The best part about RoR is that according to google ten thousand sites have the answer you're looking for, but when you try to visit them you get nothing but that stellar "Application Error". Does a lot to instill confidence!
soma lkzx
Your advice was perfect, soma lkzx, and for other's benefit, I did need to handle the exception: def self.add_sample_user_association(params) begin Sample.find_by_id(params[:sample][:id]).users << User.find_by_id(params[:user_id]) return true rescue ActiveRecord::StatementInvalid => error return false end end
Blazecock Pileon
Related Q & A:
- Rails: How to use ActionMailer by itself?Best solution by stackoverflow.com
- Is there email validation baked in to Rails 3?Best solution by coverhound.com
- how to use ajax with json in ruby on rails?Best solution by Stack Overflow
- How many rake tasks can be run at once in rails?Best solution by stackoverflow.com
- Can you hit rails on the ride antic snowboard?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.