using the geocoder gem
Can someone tell me can i get the City and State to automatically be entered into the database if i add those columns.
in the example below i am passing in :cs witch is an address
class Location < ActiveRecord::Base
attr_accessible :cs, :latitude, :longitude
geocoded_by :cs
after_validation :geocode, :if => :cs_changed?
has_many :shipments, :foreign_key => :origin_id
has_many :shipments, :foreign_key => :dest_id
end
I need to save the geocoded city and state that was parsed.
either that or figure out how to get the gecoder to give me a list of locations in a particular group of states from the database
I would be passing in array [mo,il,ks,ar] or string "mo,il,ks,ar"
i have tried:
Location.near([mo,il,ks,ar])
I can however do:
Location.near("springfield, mo", 20)
I figured it out after alot of research i added the columns City and State and did this:
Actual Working Code: this will add the City and State from the Results...
class Location < ActiveRecord::Base
attr_accessible :cs, :latitude, :longitude, :city, :state
after_validation :geocode, :if => :cs_changed?
has_many :shipments, :foreign_key => :origin_id
has_many :shipments, :foreign_key => :dest_id
geocoded_by :cs do |obj, results|
geo = results.first
if geo
obj.latitude = geo.latitude
obj.longitude = geo.longitude
obj.city = geo.city
obj.state = geo.state_code
end
end
end
Would also like to say that my input field is utilizing the GeoComplete Gem that gives valid City, St, Country combinations before it gets here so should be able to find the right location
Related
How would I make this work? I want to validate an award nomination based on criteria from a different model.
class Award < ActiveRecord::Base
belongs_to :manager, :class_name => 'Manager', :foreign_key => 'manager_username'
def cant_be_manager
if nominee_username == Manager.username
errors.add(:nominee, "is a manager and cannot be nominated.")
end
end
end
Try this:
class Award < ActiveRecord::Base
belongs_to :manager, :class_name => 'Employee', :foreign_key => 'manager_username'
validate :cant_be_manager # <----- added this line
def cant_be_manager
if nominee_username == manager.username # <----- lower case m
errors.add(:nominee, "is a manager and cannot be nominated.")
end
end
end
But (just guessing here what your model looks like) I'm wondering if that second modified line shouldn't be:
if nominee_username == manager_username
The belongs_to line indicates that you have a manager_username field in your awards table, but it would be more common in Rails for this to be a manager_id field, with the belongs_to line looking like this:
belongs_to :manager, :class_name => 'Employee', :foreign_key => 'manager_id'
If that is indeed what you have, your code should look like this:
class Award < ActiveRecord::Base
belongs_to :manager, :class_name => 'Employee', :foreign_key => 'manager_id' # <----- changed
validate :cant_be_manager # <----- added this line
def cant_be_manager
if nominee_id == manager_id # <----- changed
errors.add(:nominee, "is a manager and cannot be nominated.")
end
end
end
This assumes that you are trying to prevent an employee from nominating his own manager, but it's okay for the employee to nominate other managers, or for managers to nominate other managers. If instead you want to prevent any managers at all from being nominated by anyone, let me know how you know if an Employee is a manager (probably an attribute or method on your Employee model) and I will update the answer.
Maybe smth. like this?
class Award < ActiveRecord::Base
validate :cant_be_manager
def cant_be_manager
.....
end
See this question too: Rails custom validation
I have an application that is for sales reps.
I'm trying to pull different institutions information based on 2 things
If they are a client
If they are in the state that the user (sales rep) is over.
So I want to show all the institutions that are clients in the current_user (sales reps) area. How can I do that?
Since I haven't done this before and I'm newer to rails I'm not sure how to do this.
Here are my models (I've shortened the code):
User(Sales Rep)
class User < ActiveRecord::Base
has_many :states, :through => :rep_areas
has_many :institutions, :through => :company_reps
has_many :rep_areas
has_many :company_reps
end
States
class State < ActiveRecord::Base
has_many :users, :through => :rep_areas
has_many :rep_areas
has_many :institutions
attr_accessible :code, :name
end
Institutions
class Institution < ActiveRecord::Base
attr_accessible :company, :phone, :clientdate, :street, :city, :state_id, :zip, :source, :source2, :demodate1, :demodate2, :demodate3, :client, :prospect, :notcontacted
belongs_to :state
has_many :users, :through => :company_reps
has_many :company_reps
end
I'd suggest to proceed in this way:
states = current_user.states.to_a
# the following are all the Institution record in all the user's areas
inst_in_states = Institution.where(state_id: states)
# it will take an array and make an "IN" query
# the following are all the user's own clients, additionally on the states
# the user is in.
clients_in_states = current_user.institutions.where(state_id: states)
# as above, but additionally use the :company_reps join
I have two tables with a many to many relationship, through a third table. In the third table is a piece of data I need to assign when I build the relationships between the two tables, how can I use ActiveRecords build method to assign that?
Here is code to show what I mean:
class Company < Contact
has_many :contact_companies
has_many :people, :through => :contact_companies
accepts_nested_attributes_for :people, :allow_destroy => true
accepts_nested_attributes_for :contact_companies
end
class Person < Contact
has_many :contact_companies
has_many :companies, :through => :contact_companies
accepts_nested_attributes_for :companies, :allow_destroy => true
accepts_nested_attributes_for :contact_companies
end
class ContactCompany < ActiveRecord::Base
belongs_to :person
belongs_to :company
end
ContactCompany contains a data member called "position". What I want to do is something like:
c = Person.new
c.companies.build(:name => Faker::Company.name, :position => positions.sample)
EDIT:
When I try the code above I get "unknown attribute: position".
The c.companies.build line is attempting to build a Company object which does not have the position attribute (the ContactCompany does) hence the error. It looks like you are trying to set attributes on two different models, so you'll have to make sure you are setting the appropriate attribute on the right model:
# you can chain these calls but I separated them for readability
cc = c.contact_companies.build(:position => positions.sample)
cc.build_company(:name => Faker::Company.name)
With the following Rails models:
class Group < ActiveRecord::Base
has_many :group_locations, :dependent => :restrict
has_many :locations, :through => :group_locations, :dependent => :restrict
end
class Location < ActiveRecord::Base
has_many :group_locations, :dependent => :destroy
has_many :groups, :through => :group_locations
end
class GroupLocation < ActiveRecord::Base
belongs_to :group
belongs_to :location
end
I am trying to Find all of the locations that are associated with at least one of several groups contained in the string "group_list" (e.g. "1,2,3,4,5"). If it was a field from the Location record, I would specify a condition of "*field in (#{group_list})*". But how do I accomplish my goal when I want to have at least one of the location's "group_location" whose "group_id" is in the list (or, alternatively, one "group" whose group_id is in the list).
I know how to do it with pure SQL, but how do you do it with Rails?
#taro You are right. Started by adding the code
joins(:group_locations).where("group_id in (?)", group_id_array)
Then I proceeded to define a scope just to make it a nice package:
scope :locations_in_groups, lambda { |grparray| joins(:group_locations).where("group_id in (?)", grparray) }
Thanks for your help.
I have the following models
class User < ActiveRecord::Base
has_many :occupations, :dependent => :destroy
has_many :submitted_jobs, :class_name => 'Job', :foreign_key => 'customer_id'
has_many :assigned_jobs, :class_name => 'Job', :foreign_key => 'employee_id'
end
class Job < ActiveRecord::Base
belongs_to :customer, :class_name => 'User', :foreign_key => 'customer_id'
belongs_to :employee, :class_name => 'User', :foreign_key => 'employee_id'
belongs_to :field
end
class Occupation < ActiveRecord::Base
belongs_to :user
belongs_to :field
belongs_to :expertise
end
along with Field (just name and id) and Expertise (name and integer rank).
I need to create a filter that works like the following pseudocode
select * from jobs where employee_id == current_user_id
or employee_id == 0
and current_user has occupation where occupation.field == job.field
and if job.customer has occupation where occupation.field == job.field
current_user.occupations must include an occupation where field == job.field
and expertise.rank > job.customer.occupation.expertise.rank
You can see how I quickly exhaust my knowledge of SQL with a query this complex.
How would you do it? The proper SQL would be great, but if a Rails person can point me towards the correct way to do it with ActiveRecord methods, that's great too. Or maybe I'm not structuring my models very well; I'm open to all kinds of suggestions.
Thanks!
I might have missed something and did not look into refactoring the models but heres something that might help you to a complete solution or how to reformulate your query
The code is not tested or syntax checked
#jobs = Job.
joins(:employee,:occupation).
includes(:customer => {:occupations => :expertise}).
where(:employee_id => current_user.id).
where("occupations.field_id = jobs.field_id").all
user_occupations = current_user.occupations.include(:expertise)
user_occupations_by_field_id = user_occupations.inject({}) do |hash,oc|
hash[oc.field_id] = oc
hash
end
#jobs.reject! do |j|
common_occupations = j.customer.occupations.select do |oc|
if c = user_occupations_by_field_id[oc.field_id]
!user_occupations.select do |eoc|
c.field_id == eoc.field_id && c.expertise.rank > oc.expertise.rank
end.empty?
else
true
end
end
end