I am using many-to-many using :through since I need to store additional information in the join table. I am doing this and it seems to work. I want to verify that this is the best way to do this. Tables foods - id, food foodallergies - food_id, symptom_id, a few other columns symptoms - id, symptom Models class Food < ActiveRecord::Base has_many :foodallergies has_many :symptoms, :through => :foodallergies end class Symptom < ActiveRecord::Base has_many :foodallergies has_many :foods, :through => :foodallergies end class Foodallergy < ActiveRecord::Base belongs_to :foods belongs_to :symptoms end I have a JournalEntry model that stores foods and symptoms as comma-seperated strings. Upon record creation in the JournalEntry, I want to parse the foods and symptoms and create a many to many relationship between them. I do this like this in the create method in the JournalController def create params[:journal_entry]["user_id"] = @session[:user].id @journal_entry = JournalEntry.new(params[:journal_entry]) if @journal_entry.save # parse the foods and create a new record if food not already in db params[:journal_entry]["eat"].split(%r{,\s*}).each do |food| f = Food.find(:first, :conditions => ["food=?", food.strip]) unless f f = Food.create(:food => food) end params[:journal_entry]["symptoms"].split(%r{,\s*}).each do |symptom| s = Symptom.find(:first, :conditions => ["symptom=?", symptom.strip]) unless s s = Symptom.create(:symptom => symptom) end # create join f.foodallergies.create(:food_id => f.id, :symptom_id => s.id) end end flash[:notice] = ''JournalEntry was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end Is this the best way to do it? Since I am using create, there is a database interaction on every ''create''. Is there a way to leverage ActiveRecord functionality to batch the database interaction? Thanks in advance, Vaishal -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/ea7951de/attachment.html
I can''t even get :through to work. I get this error: ''Unknown key(s): through'' Can anyone help? Thanks! -- Posted via http://www.ruby-forum.com/.
Vaishal Sheth wrote:> I am using many-to-many using :through since I need to store additional > information in the join table. > > I am doing this and it seems to work. I want to verify that this is the > best > way to do this.Your has_many :through code looks solid to me. You might want to check the association examples on my blog if you run into trouble. I''ll be doing another one soon for working with attributes in a join model.> Is this the best way to do it? Since I am using create, there is a database > interaction on every ''create''. Is there a way to leverage ActiveRecord > functionality to batch the database interaction?Wrap all the calls in a transaction. (JournalEntry.transaction do ... end) When you commit the transaction all the new records will be saved en masse. You may also be able to get some leverage by using build() instead of create(), then when you call save on the parent object the children objects should be saved along with. I haven''t used the latter feature with :through associations yet, so I''m not sure how well that works. But the transaction thing should help a lot. By the way, I suggest keeping the user_id in the session, not the user object itself. It will save you a world of trouble trying to keep things in sync. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Thanks Josh. Nice example on your blog. May I suggest a tutorial on data entry using through with examples? A lot of people have posted examples about how to retrieve data using :through but I couldn''t find any on data entry issues. Transactions..aha, I will try that out. BTW, if instead of using create, I used the new method for foods, symptoms and foodallergies, would that work? Would rails then save everything in a batch? or would it not even allow me to do it because just using new will not get me an id for food or symptom and hence it wouldn''t know what to insert in the association? Vaishal On 4/4/06, Josh Susser <josh@hasmanythrough.com> wrote:> > Vaishal Sheth wrote: > > I am using many-to-many using :through since I need to store additional > > information in the join table. > > > > I am doing this and it seems to work. I want to verify that this is the > > best > > way to do this. > > Your has_many :through code looks solid to me. You might want to check > the association examples on my blog if you run into trouble. I''ll be > doing another one soon for working with attributes in a join model. > > > Is this the best way to do it? Since I am using create, there is a > database > > interaction on every ''create''. Is there a way to leverage ActiveRecord > > functionality to batch the database interaction? > > Wrap all the calls in a transaction. (JournalEntry.transaction do ... > end) When you commit the transaction all the new records will be saved > en masse. You may also be able to get some leverage by using build() > instead of create(), then when you call save on the parent object the > children objects should be saved along with. I haven''t used the latter > feature with :through associations yet, so I''m not sure how well that > works. But the transaction thing should help a lot. > > By the way, I suggest keeping the user_id in the session, not the user > object itself. It will save you a world of trouble trying to keep things > in sync. > > -- > Josh Susser > http://blog.hasmanythrough.com > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/983ca750/attachment.html
are you are using Rails 1.1? On 4/4/06, Sutee Dee <thaiyoshi@gmail.com> wrote:> > I can''t even get :through to work. > > I get this error: ''Unknown key(s): through'' > > Can anyone help? > > Thanks! > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/d65f018e/attachment.html
Apparently Analagous Threads
- Record retrieval in Many-to-many using :through not working
- has_many & belongs_to with Plugins (login_engine)
- acts_as_taggable plugin to tag multiple fields in a model
- Sanitize my params and the create method
- Doing and storing a Writeboard like diff in RoR