Rails 3.2 M-N association search geocoder - ruby-on-rails

I have following 2 models
class Watersporttyp < ActiveRecord::Base
attr_accessible :name
has_many :activities
has_many :watersportspots, through: :activities
end
class Watersportspot < ActiveRecord::Base
attr_accessible :watersporttyp_ids, :latitude, :longitude, address
belongs_to :user
has_many :activities
has_many :watersporttyps, through: :activities
reverse_geocoded_by :latitude, :longitude
end
In my View I have this search form
<%= form_tag watersportspots_path, :method => 'get' do %>
<div class="row collapse">
<div class="small-10 columns">
<%= text_field_tag :search, params[:search] %>
</div>
<div class="small-2 columns">
<%= submit_tag "Search", :name => nil, :class => "button prefix" %>
</div>
</div>
<div class="row">
<div class="small-12 columns">
<% Watersporttyp.all.each do |watersporttyp| %>
<%= check_box_tag "typs[]", watersporttyp.id %>
<%= watersporttyp.name %><br>
<% end %>
</div>
</div>
<% end %>
My question is how do I set up my controller to perform search by filtering nearby location with the attributes from the checkboxes "types[]"
I tried this in my controller, but it doesn't work
#watersportspots = Watersportspot.near(params[:search], 50, :order => :distance, :units => :km).join(:watersporttyp).where(:watersporttyp => params[:typs])
I'm using the geocoder gem for geocoding and my system is using ruby 1.9.3 and Rails 3.2

Related

How can I avoid ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection errors in my setup?

I have the following models in my rails app:
class SaleContact < ActiveRecord::Base
validates :key_contact_id, presence: true, uniqueness: { scope: :sales_opportunity_id, message: "Contact already added!" }
validates :sales_opportunity_id, presence: true
belongs_to :key_contact, inverse_of: :sale_contacts
belongs_to :sales_opportunity, inverse_of: :sale_contacts
has_many :phone_numbers, :through => :key_contact
accepts_nested_attributes_for :phone_numbers
end
I'm trying to create a sale_contact from the sales_opportunity screen by selecting a key_contact (assume a key_contact already exists). I would also like the ability to add a phone_number at the same time using fields_for and nested attributes.
class KeyContact < ActiveRecord::Base
validates :first_name, :last_name, :company_id, presence: true
has_many :phone_numbers, dependent: :destroy
belongs_to :company
has_many :sales_opportunities, :through => :sale_contacts
has_many :sale_contacts, dependent: :destroy
end
Assume I've already created the key_contact and assigned it to the company that owns it.
class PhoneNumber < ActiveRecord::Base
validates :number, :key_contact_id, presence: true
belongs_to :key_contact
end
Nothing magical here - just a very basic model for a phone number.
On the sales_opportunity page I can load a modal that adds a new sale_contact. It's a bootstrap modal loaded by AJAX, but I don't think that matters much (I've only included the form parts for brevity):
<%= form_for(#sale_contact, :html => {role: :form, 'data-model' => 'sale_contact'}, remote: true) do |f| %>
<% if #sale_contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale_contact.errors.count, "error") %> prohibited this sale_contact from being saved:</h2>
<ul>
<% #sale_contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group" id= "contact_error">
<%= f.label :key_contact_id, :class => "control-label" %>
<div id="contact_select">
<%= f.collection_select :key_contact_id, #sales_opportunity.company.key_contacts(:full_name), :id, :full_name %>
</div>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<div class="form-group">
<%= f.label :role, :class => "control-label" %>
</br>
<%= f.select(:role, options_for_select(#roles.collect { |r| [r[0].humanize, r[0]] }, selected: #sale_contact.role), {}) %>
<span class="help-block"></span>
</div>
<div class="form-group">
<%= f.label :preference, :class => "control-label" %>
</br>
<%= f.select(:preference, options_for_select(#preferences.collect { |r| [r[0].humanize, r[0]] }, selected: #sale_contact.preference), {}) %>
<span class="help-block"></span>
</div>
<%= f.fields_for(:phone_numbers) do |phone| %>
<div class="form-group">
<%= phone.label :number, "Phone Number", :class => "control-label" %>
</br>
<%= phone.text_field :number, :placeholder => "Enter phone number (optional)" %>
<span class="help-block"></span>
</div>
<div>
<%= phone.hidden_field :key_contact_id %>
</div>
<% end %>
<div class="form-group">
<%= f.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
</div>
<%= f.submit "Save", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }%>
<% end %>
And from my sale_contact_controller new action:
def new
#sale_contact = SaleContact.new
#sale_contact.phone_numbers.build
#sales_opportunity = SalesOpportunity.find(params[:sales_opportunity_id])
#company = #sales_opportunity.company
#roles = SaleContact.roles
#preferences = SaleContact.preferences
render :modal_form
end
def sale_contact_params
params.require(:sale_contact).permit(:key_contact_id, :sales_opportunity_id, :role, :preference, phone_numbers_attributes: [:number, :id])
end
I run a javascript snippet when the modal is being retrieved via AJAX or the key_contact select dropdown changes to bring in the key_contact_id to the phone_numbers_attributes; if I don't do that I get a 422 unprocessable entity error for not sending through my key_contact_id.
Using this setup the modal pops into view and has the correct fields (both for sale_contact and phone_number), but will not save either of these models - I get a 500 error:
Completed 500 Internal Server Error in 17ms
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection (Cannot modify association 'SaleContact#phone_numbers' because the source reflection class 'PhoneNumber' is associated to 'KeyContact' via :has_many.):
app/controllers/sale_contacts_controller.rb:50:in `block in create'
app/controllers/sale_contacts_controller.rb:49:in `create'
I've tried other methods, such as setting the key_contact_id in the sale_contact create method, but I get the same result. I can't work out why this setup complains when I don't pass a key_contact_id (422 error) and complains when I do (500 internal server error as the key_contact_id is being set internally by the rails associations).
What does Rails want me to do here?
I had a similar issue with my form, but I was able to get passed it by using nested field_for tags
Proposal.rb
has_many :proposal_section_reviews, :through => :proposal_review_status
has_one :proposal_review_status, :dependent => :destroy
attr_accessible :proposal_review_status_attributes, :proposal_review_status_attributes, :proposal_section_reviews_attributes
accepts_nested_attributes_for :proposal_review_status, :proposal_section_reviews
ProposalSectionReview.rb
belongs_to :proposal_review_status
ProposalReviewStatus.rb
has_many :proposal_section_reviews, :dependent => :destroy
_form.html.haml
= form_for proposal do |f|
= f.fields_for :proposal_review_status do |ff|
= ff.fields_for :proposal_section_reviews
So in your instance I would try
<%= f.fields_for(:key_contact) do |key| %>
<%= key.fields_for(:phone_numbers) do |phone| %>
<div class="form-group">
<%= phone.label :number, "Phone Number", :class => "control-label" %>
</br>
<%= phone.text_field :number, :placeholder => "Enter phone number (optional)" %>
<span class="help-block"></span>
</div>
<div>
<%= phone.hidden_field :key_contact_id %>
</div>
<% end %>
<% end %>
Hope this helps

Works for 'edit', fails for 'new'... undefined method `association' for #<ActionView::Helpers::FormBuilder:

Following the railscast #196 on nested forms... I have the following models:
class DealContact < ActiveRecord::Base
belongs_to :deal
belongs_to :contact
class Contact < ActiveRecord::Base
has_many :deal_contacts
has_many :deals, through: :deal_contacts
accepts_nested_attributes_for :deal_contacts, :allow_destroy => true
class Deal < ActiveRecord::Base
has_many :deal_contacts
has_many :contacts, through: :deal_contacts
accepts_nested_attributes_for :deal_contacts, :allow_destroy => true
In my deals form I have this
<div class="row">
<div class="span12"><h4>Contacts Associated with this Deal</h4></div>
<%= f.fields_for :deal_contacts do |builder| %>
<%= render 'deal_contact_fields', f: builder %>
<% end %>
<div class="span1"><%= link_to_add_contact "Add", f, :deal_contacts %></div>
</div>
</div>
And deal_contact_fields just contains:
<fieldset>
<div class="span4">
<%= f.association :contact, collection: Contact.all(order: 'contact_name'), label_method: :full_desc %>
</div>
<div class="span6">
<%= f.label :details, "Details " %>
<%= f.text_field :details %>
</div>
<div class="span1" style="margin-top: 30px">
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</div>
</fieldset>
This all works great for editing existing deals, however when I create a new one I get the following error:
undefined method `association' for #<ActionView::Helpers::FormBuilder:0x007fe6fba55840>
:-(
.association is only available in simple_form gem
You'll have to use collection_select to achieve what you want without that gem:
<%= f.collection_select :contact, Contact.all(order: 'contact_name'), :id, :name %>

Nested attributes on Rails: Can't mass-assign protected attributes

I've been stuck on getting nested attributes to work on Rails 3.2.14 for a while and having looked at many examples I still can't seem to get it to work. At the moment when I try to submit the form I get the following error:
ActiveModel::MassAssignmentSecurity::Error in Admin::CategoriesController#create
Can't mass-assign protected attributes: products, category_departments
Here's my code:
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :category_name, use: :slugged
attr_accessible :category_name, :products_attributes, :slug, :department_id, :category_departments_attributes
has_many :products
has_many :category_departments
has_many :departments, :through => :category_departments
validates_presence_of :category_name
accepts_nested_attributes_for :products
accepts_nested_attributes_for :category_departments
scope :department_category, lambda {|department| joins(:department, :products).where("departments.department_name" => department ) }
end
Controller:
class Admin::CategoriesController < ApplicationController
def new
#category = Category.new
#category.products.build
#category.category_departments.build
end
def create
#category = Category.new(params[:category])
if #category.save
redirect_to admin_products_path
else
flash.now[:error] = "Could not save the category"
render "new"
end
end
Form-view
So I finally solved this by removing the products fields_for and allowing the department to be selected from the products creation page.
Form:-
<h1> Create a Category </h1>
<%= form_for :category, :url => { :action => "create" }, :method => :post do |f| %>
<%= render :partial => 'form_category', :locals => {:f => f} %>
<% end %>
Partial:-
<div class="category-form-update">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">
<%= f.label :category_name %>
</label>
<div clas="controls">
<%= f.text_field :category_name %>
</div>
</div>
<div class="control-group">
<%= f.fields_for :category_departments do |builder| %>
<label class="control-label">
<%= builder.label :department, "Department" %>
</label>
<div class="controls">
<% department = Department.all.map { |dep| [dep.department_name.capitalize, dep.id] } %>
<%= builder.select(:department_id ,options_for_select(department)) %>
<% end %>
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn">
<%= f.submit %>
</button>
</div>
</div>
</div>
What am I not doing correctly here?
I think this is as easy as adding :products and :category_departments to attr_accessible like so:
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :category_name, use: :slugged
attr_accessible :category_name, :products_attributes, :slug, :department_id, :category_departments_attributes, :products, :category_departments
...
Without checking the internals, I assume this is necessary because the nested_attributes magic uses the :products_attributes to populate :products.
EDIT:
This only revealed the actual issue which is the nested attribute naming magic didn't kick in and name the fields properly. My guess is the form_for doesn't have the category object as a parameter.

Rails 3.1 - Editing Attributes in join models?

I'm having real trouble getting my head around editing attributes in has_many through join models. I've set up a very simple app to experiment with; Recipes, Ingredients and Recipe_Ingredients (the join).
Can anyone help with making this work as it should? As it is, it'll pulling through 'qty' from the join model, but not the actual ingredient.
I've put a public repo up that anyone can download to play with: https://github.com/EssentialMusic/Recipes
The models:
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_ingredients, :dependent => :destroy
has_many :recipes, :through => :recipe_ingredients
end
class Recipe < ActiveRecord::Base
attr_accessible :author, :description, :name, :recipe_ingredients_attributes, :ingredients_attributes
has_many :recipe_ingredients, :dependent => :destroy
has_many :ingredients, :through => :recipe_ingredients
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
accepts_nested_attributes_for :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
attr_accessible :measure, :qty, :special_instructions
end
The form
<%= form_for(#recipe) do |f| %>
<% if #recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% #recipe.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div>
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= ri.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Cheers!!
Substitute the ri with f in the second nested fields_for like this:
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= **f**.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>

has_one with :class_name, and belongs_to relation not setting attributes correctly in rails 3.2.3

I'm trying to make a form to set values to two children objects through has_one (with :class_name option) and belongs_to relation. However, when I input and submit values through the form, both children objects have the same value even if I input different values.
I have the these two models.
(two children objects above indicates "origin" and "destination" whose class names are "Place")
class Route < ActiveRecord::Base
attr_accessible :name, :destination_attributes, :origin_attributes
has_one :origin, :class_name=>"Place"
has_one :destination, :class_name=>"Place"
accepts_nested_attributes_for :origin, :destination
end
class Place < ActiveRecord::Base
attr_accessible :address, :lat, :lng, :name, :route_id
belongs_to :route, :foreign_key => "route_id"
end
And made form using partial like following.
routes/_form.html.erb
<%= form_for(#route) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :origin, place_object: #route.origin, parent_form: f} %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :destination, place_object: #route.destination, parent_form: f} %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
places/nested_places_form.html.erb
<%= parent_form.fields_for record_name, place_object do |t| %>
<%= record_name %>
<% if place_object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#place.errors.count, "error") %> prohibited this place from being saved:</h2>
<ul>
<% #place.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= t.label :name %><br />
<%= t.text_field :name %>
</div>
<div class="field">
<%= t.label :lat %><br />
<%= t.text_field :lat %>
</div>
<div class="field">
<%= t.label :lng %><br />
<%= t.text_field :lng %>
</div>
<% end %>
Like I mentioned, the attributes of origin and destination always end up the same even when I put different values in blanks and submit from form.
How can I make this work?
Somehow you'll need to differentiate between origin and destination in the database. If they both have the same class and are stored in the same table, there's nothing to tell them apart. If you don't want to change the existing relationships, you might need to use STI for this and make the origin and destination different classes:
class OriginPlace < Place
end
class DestinationPlace < Place
end
class Route < ActiveRecord::Base
...
has_one :origin, :class_name=>"OriginPlace"
has_one :destination, :class_name=>"DestinationPlace"
...
ene
This will require a type field in the places table.

Resources