Is it possible to validate a grouped_collection_select in Rails? - ruby-on-rails

I am working on a Rails app where users can post Quotes about different art forms (e.g. Music, Painting, Photography, etc.). Each Quote must be assigned a Medium (such as Music or Photography) and a Genre (such as Jazz or Rock and Roll for music or Landscape for photography).
I am using a grouped_collection_select for the Genre field on my Quote form, and that works great to sort the genres, but I'd like to prevent anyone from selecting a genre that doesn't belong to the medium they have selected.
I know that I can do this dynamically with javascript, as in this Railscast, but I'd like to create a validation to ensure that no bad data gets into the database.
Is there a way to validate this in my Quote model so that there's no way a Quote can be saved with a genre that does not have the correct medium? This would prevent someone from saving something that is the medium "Photography" and the genre "Jazz", for instance.
Here are my models:
class Quote < ApplicationRecord
belongs_to :medium
belongs_to :genre
end
class Medium < ApplicationRecord
has_many :quotes
has_many :genres
end
class Genre < ApplicationRecord
has_many :quotes
belongs_to :medium
end
And here are the fields on my Quote form:
<%= f.label :medium, 'Medium' %>
<%= f.collection_select :medium_id, Medium.order(:name), :id, :name, {prompt: 'Select a medium'}, {class: 'form-control'} %>
<%= f.label :genre, 'Genre' %>
<%= f.grouped_collection_select :genre_id, Medium.order(:name), :genres, :name, :id, :name, {prompt: 'Select a genre'}, {class: 'form-control'} %>

You can use rails validate method to achieve this,
validate: check_medium
def check_medium
errors.add(:base, "Your error message") if genre.try(:medium) != medium
end

My proposal here is to story only genre_id in quotes table. This belongs_to :medium is unnecessary because each genre already knows its medium. With architecture like this, you don't have to worry about medium-genre mismatch at all. Models:
class Quote
belongs_to :genre
# With delegation you still can do something like quote.medium
delegate :medium, to: :genre
end
class Medium
has_many :genres
has_many :quotes, through: :genres
end
class Genre
belongs_to :medium
has_many :quotes
end

Related

How to work with rich join tables in Rails

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!!!

rails: using collection_check_boxes in each category

Using
<%= collection_check_boxes(:employee, :skill_ids, Skill.all, :id, :name) %>
on its own works just fine in my project. This way, each employee shows all skills and if they are checked or not.
Additionally to that, I have some categories, which the skills are related, to (Category has many Skills).
Can anyone recommend a proper way, to use the collection_check_boxes correctly for each category so only the skills related to the category are displayed?
I think I am just missing the correct limitation for the "Skill.all" parameter.
Finally it should look like this, but instead of Submit buttons (by using jquery-ujs, method: put), I want those skills to be checkboxes.
http://d.pr/i/CYHaGH
Edit1: Skills and Categories model
class Category < ApplicationRecord
has_many :subcategories, class_name: "Category", foreign_key: "parent_id"
belongs_to :topcategory, class_name: "Category"
has_many :skills
end
class Skill < ApplicationRecord
has_many :employeeskillsets, foreign_key: "skill_id"
has_many :employees, through: :employeeskillsets
has_many :projectskillsets
has_many :projects, through: :projectskillsets
has_many :tagsets, dependent: :destroy
has_many :tags, through: :tagsets
belongs_to :category
end
Edit2: Still no progress. Does anybody have an idea how this could be done?
SOLUTION:
Ok, the solution was pretty simple. I just had to declare the collection on each category, which already was for a previous version of that app. <%= collection_check_boxes :employee, :skill_ids, category.skills, :id, :name%> (category.skills was set for each instance of categories).
Thanks for the help.
Using collection_check_boxes, you can pass a block like that :
collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
b.label { b.check_box }
end
And you can access value, text and more :
collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b|
b.label(:"data-value" => b.value) { b.check_box + b.text }
end
You should read the documentation here :
https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
You will find all you need to solve your issue,
Hope that help
Ok, the solution was pretty simple. I just had to declare the collection on each category, which already was for a previous version of that app. <%= collection_check_boxes :employee, :skill_ids, category.skills, :id, :name%> (category.skills was set for each instance of categories).

Using join model with nested form (has_many :through relationship)

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

How to reference attributes in deep associations

Summary
Rails 3.2 with RefineryCMS 2.0. These are my pseudocode models:
Industry
name
has_many companies
has_many works through companies
Company
name
has_many works
belongs_to industry
Work
name
belongs to company
From an instance of Work, I can say work.company.name and get a the name of the associated company. I would expect it to follow that I could also say company.industry.name without a problem. However, I am getting an unhelpful error:
wrong constant name Refinery:Industries
What I would ultimately like to do is follow my associations all the way up ie work.company.industry.name, but the chain is broken between company and industry it seems.
What am I doing wrong here? Here's my code in more detail.
Code
Here are my models. Any idea what would prevent me from accessing industry attributes from an associated company given that industries have_many companies (companys lol) and companies belong_to an industry? Any help would be much appreciated.
Industry Model
module Refinery
module Industries
class Industry < Refinery::Core::BaseModel
...
attr_accessible :name, :description, :position
has_many :companys, :class_name => '::Refinery::Companys::Company', :dependent => :nullify
has_many :works, :through => :companys
end
end
end
Company Model
module Refinery
module Companys
class Company < Refinery::Core::BaseModel
...
attr_accessible :name, :position, :industry_id
has_many :works, :class_name => '::Refinery::Works::Work', :dependent => :destroy
belongs_to :industry, :class_name => '::Refinery:Industries::Industry'
end
end
end
Work Model
module Refinery
module Works
class Work < Refinery::Core::BaseModel
...
attr_accessible :name, :description, :position, :company_id
belongs_to :thumbnail, :class_name => '::Refinery::Image'
belongs_to :Company, :class_name => '::Refinery::companys::company'
end
end
end
Then in my erb file I'm doing this:
<% #works.each do |work| %>
...
<h5>
<%= work.company.name %>
</h5>
<% end %>
That one works.
This one gives me an error though:
<% #clients.each do |client| %>
<h5>
<%= client.industry.name %>
</h5>
<% end %>
That error reads:
wrong constant name Refinery:Industries
There is at least a double colon missing in your Company model:
belongs_to :industry, :class_name => '::Refinery:Industries::Industry'
should be
belongs_to :industry, :class_name => '::Refinery::Industries::Industry'
I haven't really looked at the rest of the code, but this is a first error.

Data Modeling Structure - Rails

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.

Resources