0

Suppose that I've two controllers, "good" and "customer" and that I want add many goods to a single customer; in which way can I do this?

I mean, I need a customer's show view that list all goods associated to that project and a "add good to customer" used to add a new object to customer.

lucapette
  • 20,564
  • 6
  • 65
  • 59
Marco
  • 10,283
  • 4
  • 23
  • 22
  • 1
    http://guides.rubyonrails.org/association_basics.html – Reactormonk Feb 24 '12 at 14:25
  • Sorry, I've explained me bad. My association is the following: 1 good <-> multiple customers and vice versa 1 customer <-> 1 good .As I wrote before customer's show view must list all goods associated and a "add good to customer" used to add a new object to custome. Is it change anything – Marco Feb 24 '12 at 15:05

1 Answers1

0

You would want to do that at the model level with an association and then allow for nested attributes.

It might look like this

model

class customer < ActiveRecord::Base
    has_one :goods
    accepts_nested_attributes_for :goods
end

Then in the views for customer you would want to have the nested form. This Railscast gives a good overview for that. Because of the association and the accepts you can automatically inherit them in the controller. Also there is a similar question here that amplifies the explanation.

Community
  • 1
  • 1
ScottJShea
  • 7,041
  • 11
  • 44
  • 67
  • Sorry, I've explained me bad. My association is the following: 1 good <-> multiple customers and vice versa 1 customer <-> 1 good .As I wrote before customer's show view must list all goods associated and a "add good to customer" used to add a new object to custome. Is it change anything? – Marco Feb 24 '12 at 15:02
  • @Marco So goods can have multiple customers and customers can have multiple goods? – ScottJShea Feb 24 '12 at 15:19
  • No! goods can have multiple customers BUT customer can have ONLY one good. To understand better let's say that in details, good are pc hardware's model for me while customers are my client and let's say that I'm creating a web app to manage my stores.It's always a one to has_many relationship – Marco Feb 24 '12 at 15:29
  • @Marco There are some slight changes. I updated my answer to reflect that – ScottJShea Feb 24 '12 at 15:44
  • OK thank you!Another question,I've seen that you've wrote "has_one :goods" but in class goods I've always the following right?class good < ActiveRecord::Base has_many :customers end – Marco Feb 24 '12 at 15:48