On 7/6/06, Noah Keen <nkeen@firstlook.biz> wrote:>
> Hi,
>
>
>
> Day 3 with rails here. Forgive me. If this has been covered many times
> before, pls just point me at the article.
>
>
>
> I worked through the Rolling with Rails tutorial (
> http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html) built up my
> cookbook. Things work great.
>
>
>
> I now want to add Ingredients to my app. So, a Recipe will have 1 or more
> Ingredients and any single ingredient can be referenced (belong to?) any
> recipe.
>
>
>
> How do I set up this relationship? With other languages I would create a
> mapping table in my database which had two columns, a Recipe index and
> Ingredient index. I could then write some simple SQL to pull all the
> Ingredients for my Recipe or to get all the Recipes containing a specific
> Ingredient. It doesn''t look like this is how it happens in rails.
>
You can use this approach with has_and_belongs_to_many
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000533
Alternativley you can use has_many :through
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530
Josh Susser''s blog has some good information on has_many :through and
has_and_belongs_to_many
http://blog.hasmanythrough.com/
Do I modify my Ingredient class to have a ''belongs_to_many''
relationship to> Recipe and Recipe to have a ''has_many'' relationship to
Ingredients? How is
> this mapping done on the DB? What fields in the tables do I need to add? Am
> I able to easily query the stuff mentioned above?
>
>
>
> Thanks in advance,
>
> Noah
>
>
>
> _______________________________________________
> 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/20060705/855b75da/attachment-0001.html