Matt Miller
2006-May-24 17:53 UTC
[Rails] Has Many Through + Join Model + Forms + Confused?
Hello, I''m trying to implement something like the following scenario. I''ve got "Alloys" (blends of metals), "Metals" and "Percentages" I want to implement this using a join model, not using a has_and_belongs_to_many relationship. (If i''m wrong about that, let me know). Basically, i have the following tables: Metals id name Alloys id name Percentages alloy_id metal_id percent And the following code in my models: Alloy has_many :metals, :through => :percentages has_many :percentages, :dependent => true Metal has_many :alloys, :through => :percentages has_many :percentages, :dependent => true Percentage belongs_to :alloy belongs_to :metal Now, I''m trying to make what boils down to an interface for people to create new alloys. Alloys can have up to a predefined amount of metals (not infinite, like tags, but it does need to be extendable, otherwise i could go for a less elegant database design and throw out all the relational database ideas. In other words, i could just make the table "Alloys" have Metal1, Percentage1, Metal2, Percentage2, etc. as columns in the table). Ok, so, what''s the right way to do this? I was hoping to be able to put in, say, 6 (where you can have 6 metals) select boxes, each listing all the metals, and have 6 text fields to allow the user to type in the percentage of each. Just as a start, i tried to do something like: <%= select(''alloy'', ''NNNNN'', Metal.find(:all, :order => "name").map {|v| [ v.name, v.id]}) %> where NNNNN is any number of things i''ve tried, all of which give me a "NoMethodError in Alloys#new" error. Am I on the fringes of rails? Or am I so newb it hurts? Any help would be appreciated. -Matt Miller -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060524/75ba8a12/attachment.html
Daniel Burkes
2006-May-24 21:31 UTC
[Rails] Re: Has Many Through + Join Model + Forms + Confused?
What''s this? A HMT question without a lightning-fast response from Josh, the Lord of the Joins? Sorry, couldn''t resist :-) -- Posted via http://www.ruby-forum.com/.
Matt Miller
2006-May-24 22:11 UTC
[Rails] Re: Has Many Through + Join Model + Forms + Confused?
I''ve "solved" the problem by essentially making an "Edit Metals List" on my Alloys listing page. I map that into: <%= link_to ''Edit Metals List'', :controller => ''percentages'', :action => ''listfromalloy'', :id => alloy %> Then, the listfromalloy action is: def listfromalloy @alloy = Alloy.find(params[:id]) @percentages = @alloy.percentages end That essentially does the same thing as the standard "list" action, just tells it to get only the metals already associated w/ that alloy. This "works" but it requires that the user create an alloy, then create the associations. I''d like this to be an "all in one" process, through one form. (I.e., the alloys "new" action / _form.rhtml has some drop downs and text boxes that let the user say what the associated percentages are) Thanks! -Matt Miller On 5/24/06, Daniel Burkes <dburkes@netable.com> wrote:> > What''s this? A HMT question without a lightning-fast response from > Josh, the Lord of the Joins? > > Sorry, couldn''t resist > > :-) > > -- > 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/20060524/69fbe01a/attachment.html