Prince Abalogu
Prince Abalogu

Reputation: 395

Using form to update has_many through join table

I am short of implementation ideas for my rails project. at this point it seems impossible to implement in rails.

I am cloning a sort of accounting software into a web app using rails framework for small manufacturing companies to keep track of their products stock in their different distribution branches.

I have 3 different models: "Product", "Branch" & "Accumulator"

class Branch < ActiveRecord::Base
has_many :accumulators
has_many :products, :through => :accumulators

end

class Product < ActiveRecord::Base
has_many :accumulators
has_many :branches, :through => :accumulators

  def self.search(search)
   if search
    where('name LIKE ?', "%#{search}%")
  end
end

class Accumulator < ActiveRecord::Base
belongs_to :product
belongs_to :branch
end

I am new to rails and after reading up on many to many associations I came across using collections to add products to a branch "@branch.products << Product.all"

Is there a possible way to use a form "number_field_tag" in the branch show view to add multiple of specific products into the join table?

eg I want to add 10 of (Product) called "garden eggs" to a (Branch) called "lagos branch" to the (Accumulator) join table using a form in Branches show view?

Upvotes: 1

Views: 221

Answers (2)

davidwessman
davidwessman

Reputation: 1228

Congratulations for choosing :has_many, through: you will not regret it.

Is the relationship between a product and a branch static? Or does it change a lot?

In your Accumulator model, add an integer field called amount (count can have conflicts). Then you either create a form for your Accumulators or you add a nested form with for example Cocoon.

This way you can add Accumulators to your Branch with a certain Product and a certain amount.

Off topic:

Here is an article about why has_many through has some advantages: http://blog.flatironschool.com/why-you-dont-need-has-and-belongs-to-many/

If you have problems with forms I can really recommend SimpleForm and for nice Javascript-assisted fields I recommend Select2.

Upvotes: 1

Arslan Ali
Arslan Ali

Reputation: 17802

If the table accumulators needs to save only two things: product_id, and branch_id, you can easily use has_and_belongs_to_many associations.

class Branch < ActiveRecord::Base
  has_and_belongs_to_many :products, join_table: 'accumulators'
end

class Product < ActiveRecord::Base
  has_and_belongs_to_many :branches, join_table: 'accumulators'
end

And now, there is no need for the third model.

As far as it goes, how to add a relationship, it's pretty easy in this case:

branch = Branch.last
branch.products << Product.create # you don't need to touch the middle table.

Instead of using number_field_tag to ask for plain ids, you can use something fancy like jQuery Chosen Plugin. This plugin will allow you to use tag like input, and will send the ids to the server separated by ,'s.

Upvotes: 0

Related Questions