0

I've been tracking the SO site as my rails progress has developed and it is a good source of information. I am writing an application to maintain a bunch of hosts and their associated warranty. contracts A host can have many contracts and a contract can apply to many hosts. So I have three classes: Host, WarrantyContract and HostWarranty. The HostWarranty model has FK references to the host and WarrantyContract as well as a comments field.

What I need to be able to do is manage the contents of the host_warranties table (assetid, warranty_contract, comments) through the host and warranty_contract pages, so that when editing a host's details, I can apply one or more contracts; when I edit a contract I can indicate what host(s) it applies to.

I have been exploring using has_many, accepts_nested_attributes_for and has_and_belongs_to_many (Many-to-many relationship with the same model in rails? being the main source so far) but I am struggling with how I develop the view _form template; most examples I have seen utilise the fields_for method, whereas what I need is a <select> list of names and id's.

I'm particularly after suggested solutionsm but more an idea of what kind of api areas I should be looking at or googling for.

Community
  • 1
  • 1
RubyNewby
  • 11
  • 2
  • Any chance you can distill that text down to a specific problem? I think I get at what you are asking but its not really clear. – Devin M Aug 06 '11 at 23:28

2 Answers2

2

I think you are asking how to multi select from existing records? If that is the case then you have an number of options. 1) You could present a list of items (contracts?) with check boxes so the user can tick the items they want

2) You could present a drop down multi select box to pick multiple items from

3) You could present a searchable input field that the user types into and as they type a list is presented that match the input criterea

All these options are covered on the Railscasts website in one shape or another

http://railscasts.com/episodes/258-token-fields http://railscasts.com/episodes/102-auto-complete-association http://railscasts.com/episodes/17-habtm-checkboxes

Should be enough there to get you started

jamesc
  • 12,423
  • 15
  • 74
  • 113
  • This is for a data collection exercise. I have been trying all the documented method for getting this working and the application is now so broken and I's so disillusioned with ruby that I have to conclude that HABTM doesn't actually work and that it will be much quicker for me to complete each asset-warranty detail for all 250 hosts once cell at a time that try to get it working in rails. – RubyNewby Aug 09 '11 at 13:40
  • The railcast fairly neatly describes part of what I need and I may, at some point in the future revisit this, but I also need to perform the operation from both hosts and contracts. Right now, too many people need this data and since they live by spreadsheets, I have failed miserably in persuading them to use a proper data driven approach. – RubyNewby Aug 09 '11 at 14:16
  • This very nearly actually worked and if I didn't have primary key fields called 'id' rather than the Rails norms then (and I do have one really stupid column name), it probably would have been successful. I have, however, hit one of those brick-wall errors ('undefined method `klass' for nil:NilClass') from which I'm unlikely to recover. I do really appreciate the suggestion and this seems the best way to solve this kind of question. Thank you. – RubyNewby Aug 09 '11 at 18:19
  • Have you got past this issue? – jamesc Aug 09 '11 at 19:26
0

The suggestion from jamesw was fantastic. Perfect. I can get some sleep now knowing that this is the way to do it. And I should be able to apply this technique to the other tables that I need to manage. The railscast is absolutely spot on. Thabk you very much.

RubyNewby
  • 11
  • 2