I am stuck struggling with rich join tables in Rails 5 and need help getting on track. The app I'm writing will help me track which of our company's suppliers carry which brands of products. Because I also need to track whether each supplier is authorized or unauthorized for each of the brands they sell, and whether they carry those brands in stock, I thought the best approach was to use a join table and store the attributes there. In other words:
Suppliers <---> Lines <---> Brands
Beyond the foreign key references for a Supplier and a Brand, the Line record also has two boolean attributes: .is_authorized and .carries_stock.
My models:
/models/supplier.rb
class Supplier < ApplicationRecord
has_many :lines, :dependent => :destroy
has_many :brands, :through => :lines
accepts_nested_attributes_for :lines
end
/models/brand.rb
class Brand < ApplicationRecord
has_many :lines, :dependent => :destroy
has_many :suppliers, :through => :lines
end
/models/line.rb
class Line < ApplicationRecord
belongs_to :supplier
belongs_to :brand
validates_presence_of :supplier
validates_presence_of :brand
end
I've been able to set up the controller and supplier edit form to allow creating records in the Lines table, but have no clue how to allow the users to edit the .is_authorized and .carries_stock attributes. I have been able to get the create/edit supplier form to work by adding the following snippet:
/views/suppliers/_form.html.erb
<h4>Brands</h4>
<%= form.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %>
<%= b.label class:"label-checkbox" do%>
<%= b.check_box + b.text%>
<%end%>
<br />
<% end %>
The form looks like this now but doesn't allow me to edit the rich attributes .is_authorized and .carries_stock. I'd like the form to look something more like this. Where do I go from here?
Thanks!!!
Related
I have a Form_for for a Show model. I would like to use a fields_for within the form_for to add bands. The thing is I don't want the fields tied to the bands when using the form to update records. If the name of the band changes I would like to update the performance with the new band.
Shows are joined with Bands through Performances
class Show < ActiveRecord::Base
has_many :performances
has_many :bands, through: :performances
accepts_nested_attributes_for :bands
end
class Band < ActiveRecord::Base
attr_accessible :name, :website, :country, :state
has_many :performances
has_many :shows, through: :performances
validates :name, presence: true, uniqueness: true
end
class Performance < ActiveRecord::Base
attr_accessible :show, :band
belongs_to :show
belongs_to :band
end
Here is my form. (simplified)
<%= form_for #show do |f| %>
#fields
<%= f.fields_for :bands do |b| %>
<%= b.text_field :name %>
<% end %>
<%end>
The problem is if this is used to change a bands name, it changes the bands name (crazy right?). I don't want it to update the Band record-- I want it to do a Band.find_or_create and update the performance record with the new band's id. This way users can replace a band in a show by removing the name and adding in another band name.
The rendered html should include the performance id not the band id (I think)
Something like:
<input id="show_performance_attributes_1_id" name="show[performance_attributes][1][id]" type="hidden" value="62">
How is this done?
Ok, so I was able to find the solution to my own question. I may have not provided enough details in the original question.
But the solution is simply to use the performance model as the nested field in the Fields_for instead of the bands model. Change the show model to accepts_nested_attributes_for performances and change the performance model to accepts_nested_attributes_for band
I have a project-specific question. I'm trying to determine the most efficient and logical way of setting up my models in my app. The relevant players to model here are: Vendors, Vendor Inventories, and Products. Here is a breakdown of what each item should be able to return in some way:
Inventories:
Store ID
Products and associated details (Price, Name, Brand, Details, Product Code)
Vendors: Store ID, Location, Name
Products: Price, Name, Brand, Details, Product Code
Obviously there is a lot of duplication in this scheme between products and inventories. My issue is that while vendors might have similar items in their inventories, the price will always be different. So I can't simply relate the models by product code. Because most vendors will have the same products, if I were to model store inventories with all of the product information, wouldn't that be a lot of duplication? It's also possible that I don't need to have a separate model for vendors and could just try and keep it all inventories, but I'm lost. Help please?! Thanks in advance.
EDIT:
Here is my model structure, though I'm not sure it's ideal.
class Vendor < ActiveRecord::Base
attr_accessible :name, :address
has_one :inventory
has_many :products, through: :inventories
end
class Inventory < ActiveRecord::Base
has_many :products
belongs_to :vendor
end
class Product < ActiveRecord::Base
attr_accessible :upc, :brand, :product, :details, :price
has_many :inventories
has_many :vendors, through: :inventories
end
I'm not sure if I understand your specific needs, especially if the set of models are just for example, but judging from the names, here would be my implementation.
A Vendor is the main entity, it will own many products through the join table inventory,
class Vendor < ActiveRecord::Base
attr_accessible :name, :address
has_many :inventories
has_many :products, through: :inventories
end
However there are aspects of the product that need to differ between Vendors, in this case, price and quantity( I just made that up), which can be stored in the join table, since it would be independent and made for each product and vendor relationship, is ideal for storing info like price and quantity.
class Inventory < ActiveRecord::Base
attr_accessible :product_id, :vendor_id, :price, :quantity
belongs_to :product
belongs_to :vendor
end
Product should only have attributes that would be universal regardless how the vendor handles it, likewise if the product where to be changed, it should be okay with all the vendors.
class Product < ActiveRecord::Base
attr_accessible :upc, :brand, :product, :details
has_many :inventories
has_many :vendors, through: :inventories
end
Doing this you may have difficulties editing this in the view, setting prices and quantity on inventory. Again, my way of doing it would be to use nested fields and interact directly with the join table, and not products, here's some quick partial code using simple_form gem,
on you form,
<%= f.simple_fields_for :inventories do |inventory_fields| %>
<%= render partial: "inventory_fields", locals: {f: inventory_fields }%>
<% end %>
<%= link_to_add_fields "Add", f, :f %>
and in inventory_fields.html.erb
<%= link_to "Remove", '#', class: "remove_fields btn btn-danger" %>
<%= f.input :product_id, collection: Product.all.somethingsomething, include_blank: false %>
<%= f.input :price %>
<%= f.input :quantity %>
Now you can choose your products from a dropdown, and set price and quantity, and whatever you need dynamically.
I have three tables via many-to-many-association: Supermarket, Product and Supply.
Each Supermarket can hold many products and each product can be sold in many supermarkets. The association is build via the Supply-model.
Supermarket:
class Supermarket < ActiveRecord::Base
attr_accessible :name, :address, :products_attributes
has_many :supplies
has_many :products, :through => :supplies
accepts_nested_attributes_for :products
end
Product:
class Product < ActiveRecord::Base
attr_accessible :name, :supermarkets_attributes
has_many :supplies
has_many :supermarkets, :through => :supplies
accepts_nested_attributes_for :supermarkets
end
Association via Supply:
class Supply < ActiveRecord::Base
attr_accessible :supermarket_id, :product_id
belongs_to :supermarket
belongs_to :product
end
I have created the scaffolds and populated the Supermarket-table.
In my Product form, i want to use one (or more) drop-down-menu(s) to select the correspondent Supermarket-name(s). Goal is to create a new product while also creating the association via the Supply-table.
What should the code look like in form and/or controller for the products if I want to select the corresponding supermarkets from there?
In you products form you need to add this line...
<%= collection_select(:product, :supermarket_ids, SuperMarket.all, :id, :name, {}, { :multiple => true } )%>
You also shouldn't need to use an accepts_nested_attributes for this, the many to many association you already have set up should take care of the rest.
I think in views
<%= f.collection_select "super_market_ids[]",#super_markets,:id,:name,{},{:multiple=>"multiple'} %>
I am not sure about super_market_ids or super_market_ids[] and syntax just verified once.
In select tag if you want checkbox type multi select there is an chosen library that will help you to build nicer UI,
I need some conceptual help:
Assume your Users are essentially a business. You have Employees and you have Staff Positions. Essentially, one employee could hold multiple positions and one position could hold multiple employees.
My have_many :through is working between the Employees and the Positions through a join table Staffization. However, my edit form for the employee is returning ALL the Positions as checkboxes for the whole app, not just the ones for this particular User. And, none are being saved when I submit an update.
Do I need to do something different with my associations, or is there a better way to narrow the data in the forms?
My models:
class User < ActiveRecord::Base
has_many :employees, :dependent => :destroy
has_many :positions, :dependent => :destroy
class Employee < ActiveRecord::Base
belongs_to :user
has_many :positions, :through => :staffizations
has_many :staffizations, :dependent => :destroy
class Position < ActiveRecord::Base
belongs_to :user
has_many :employees, :through => :staffizations
has_many :staffizations, :dependent => :destroy
class Staffization < ActiveRecord::Base
belongs_to :employee
belongs_to :position
My employee edit fields form is set up to return checkboxes for the possible positions the employee could hold but is return all the positions in the whole app and is not updating the data when I hit submit:
- Position.all.each do |position|
= check_box_tag :position_ids, position.position_name, #employee.positions.include?(position), :name => 'employee[position_ids][]'
= label_tag :position_ids, position.position_name
My employees controller update def added the line for the have_many :through association. Is this where I should narrow the return down to the current signed in user's employees and positions?
#employee.attributes = {'position_ids' => []}.merge(params[:employee] || {})
First, shouldn't you be using :
class Employee
has_and_belongs_to_many :positions
end
class Position
has_and_belongs_to_many :employees
end
then, you could narrow the available positions with :
Position.where(:user_id => #employee.user_id).each # etc.
you could even make a scope for it:
class Position
def available_for_employee employee
where(:user_id => employee.user_id)
end
end
... and then use this in a helper that generates your checkboxes
def position_checkboxes_for_employee employee
Position.available_for_employee(employee).each do |position|
= check_box_tag :position_ids, position.position_name, #employee.positions.include?(position), :name => 'employee[position_ids][]'
= label_tag :position_ids, position.position_name
end
end
returning ALL the positions as checkboxes is exactly what you'd want, no?
what if a employee changes positions? you'd need that checkbox then, not only the checked ones..
Thanks to a friend, since my have_many through between my employees and positions belongs to the business. I needed to add the attr_accessible position_ids and attr_accessible employee_ids to the respective models. In addition, in my employee view field, I needed to add the options so that my call for positions only calls those positions associated with this business, like so:
- Position.find_all_by_user_id(#employee.user_id).each do |position|
= check_box_tag :position_ids, position.id, #employee.positions.include?(position), :name => 'employee[position_ids][]'
= label_tag :position_ids, position.position_title
I created a joined table for the models category and products (both created with scaffold). The Product model is this:
class Product < ActiveRecord::Base
belongs_to :category
def category_id
category.id if category
end
def category_id=(id)
self.category = Category.find_by_id(id) unless id.blank?
end
end
and the category model is this:
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
In the form.html.erb I create a dropbox with all the classes for the user to choose from:
<p>
<label for="product_category_id">Category:</label><br />
<%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => "Select a Category" %>
</p>
Yet when I take a look at the show of the product:
<p>
<b>Category:</b>
<%= #product.category_id %>
</p>
or the list of the products (index.html.erb):
<td><%= product.category_id %></td>
There's no category. Just blank. I don't get it. Is something wrong with the category_id method or the association?
Firstly, you don't need the explicit category_id and category_id= methods. ActiveRecord will handle those for you for a belongs_to association.
Secondly, there seems to be a mismatch between whether you want a has_and_belongs_to_many or a has_many/belongs_to association. If you have a join table then you have the former, in which case both sides of the association should be declared with has_and_belongs_to_many. If you are just using a category_id on the products table then the other end of your association on Category should be has_many :products.
With a join model:
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
you would define in your Product class:
has_many :categorizations
has_many :categories, :through => :categorizations
Then, because your association is a 'many' association, you do not get a .category method on a product. You do however get a categories method (plus several more methods - look at the has_many documentation). If you name your collection_select category_ids then it should work as expected. You may also want to add the 'multiple' option to the select in order to choose more than one category.
Your association is obviously incorrect. As pointed out, the category has_many products. And in case you want to use a many-to-many relationship you're strongly advised to use the has_many :through relationship.