So I have found MANY posts on these subjects but none have answered my (probably very novice) question. I am brand new to Rails this month but I have been programming in OOL for a little while. I think my question might span multiple subjects about Ruby on Rails. I don't mind reading multiple blogs or tutorials to peace this all together.
I have 4 location models. Each have a "Name" attribute.
Country (has_many :states)
State (belongs_to :country has_many :counties has_many :cities, :through => :counties)
County (belongs_to :state has_many :cities)
City
My end goal is to have an autocomplete textbox for city, county, state. The user will select a country to narrow down the results. For example, the user will type in "SA" and the autocomplete textbox will show:
Kansas
Arkansas
Santa Clara County, California
San Jose, Santa Clara County, California
etc.
So I can easily return the text that I want displayed. Each model has something like:
def location_name
// the display name of this model. For example we want the County Model to return "Santa Clara County, California"
"#{name}, #{state.name}"
end
Solutions I tried to research:
Active Record Query Interface to make one select statement across the multiple tables. My goal would be to search across multiple models and get a single list of objects with the name containing the search text.
STI - Maybe create a Location model which all can inherit from but I couldn't find any basic tutorial online that include models with different attributes for each. Do I "generate" one model with all the fields that all 5 would need then just delete them from the auto generated .rb file? I assume I will then be able to use Location.where("name like ? and country_id = ?", params[:search], params[:country_id]) and get a full list of all location which fit the search parameters.
MTI - I looked at this since I'm not a big fan of excess columns in the DB that are mostly blank. For example :country_id would only be used by states. So it would be null for all counties and cities, etc. But all the tutorial seem to imply that STI would be the better way to go.
Polymorphic Associations - I just started looking into this a few hours ago. I found something about ":polymorphic => true" and I read another article that talked about a subclass feature.
I have spent quite a few days looking into the "right" way of doing this according to Ruby. I'd really like to learn and incorporate all that ruby has to offer. Thanks for any thought!
maybe you can watch this video for your further development too.
http://railscasts.com/episodes/88-dynamic-select-menus
one-to-many for your case should be alright i guess..
like this country has many states and the according state has many of cities.
correct me if im wrong ##
Related
I am trying to migrate some of my bugzilla data to JEERA. I have some custom fields in bugzilla which has dynamic parent-child relationship. for exa-
Suppose I have Labels "India" "China" "Russia",
when I click on Label lets say "India", then it should fetch and show only cities from India and not all cities.
Right now , I am able to create Labels and cities custome fields in jeera but lacking dynamic nature.
I will be thankful, if anyone has any idea over this.
Perhaps Select List (cascading) custom field type would best solution here.
As a bit workaround for relate two custom fields between each other you can use ScriptRunner Behaviours. It's like a Groovy definition for frontend logic. Conceptually:
City field must contains all Cities for all Countries.
Create a Behaviour for a Country field. It means when user will change/select a Country field a Behaviour will be run.
In behaviour write code that will get currently selected country and then fulfil a Cities field based on selected country.
Useful methods: getFieldById(fieldId), formField.getValue(), formField.setFieldOptions(Iterable). API Documentation.
I think #sintasy is right.
If you want N-countries have their own cities, cascading select list just fit your requrement.
If you want N-countries * M-other-things, which have no relation with countries, two select lists will fit your requrement.
If you want more complex feature, then I don't known.
I am facing Problem with Entity Framework with MVC 4 .
Let me put the Table Structure and result which i needed.
as you can see i have three table and the data structure is
can anyone suggest me how can i write Model class so that on create country page i have layout like
select Language -- Language Dropdownlist Box here
Enter Country name- country text Box here
Thanks
Ashutosh
As already said, you have problems with your table structure, and a model will reflect this, not correct it. And while I am at it, German and Chinese are languages, while Germany and China are countries.
Country Names should be in Countries table, while country_languages is a table allowing a many to many relationship and as such should never be seen by your end user.
If you define your model properly with only Countries and Languages, Entity Framework with Code First will generate a correct Countries_Languages table for you.
Don't forget that in some countries, more than one language is spoken, like Switzerland which has 4 official languages....
I have a country model and would like to display the country with the most occurrences, country names are held in the column 'mame', however the country db is pre populated and the relationship is a country
has_many recipes
and recipe
belongs_to country
So far I have
Country.group('name').order('count_name DESC').limit(1).count('name')
but this will not work will it as there are 1 of every country in the table? Do i need to do a count on the number of times the country_id is used? if so what would the syntax be for that? would it be
Recipe.group('country_id').order('count_country_id DESC').limit(1).count('country_id')
or using joins and select
Country.joins(:recipes).select('countries.*, count(country_id) as "country_count"').group(:country_id).order(' country_count desc')
Any pointers appreciated
You can do it by using queries. However, RoR has built in support to achieve the same. It is called Counter Cache.
I can explain here but I think it's better if you follow this screencast.
http://railscasts.com/episodes/23-counter-cache-column
This will give you very good idea how to use counter cache and get what you've tried to achieve.
I have used OmniAuth to connect with Facebook. I am pulling various pieces of information and have no problem pulling information where there is only one type of it. For example, to get the email address, I just place the following in user.rb:
user.email = auth["info"]["email"]
The problem is with multiple entries. For example, for education, there are two results. One is a High School named Punahou School while the other is a College named University of Illinois at Urbana-Champaign.
education:
- !map:Hashie::Mash
school: !map:Hashie::Mash
id: "105510192816251"
name: Punahou School
type: High School
- !map:Hashie::Mash
school: !map:Hashie::Mash
id: "163536409904"
name: University of Illinois at Urbana-Champaign
type: College
I was able to pull the first school using:
if auth["extra"]["raw_info"]["education"]
user.school = auth["extra"]["raw_info"]["education"][0]["school"]["name"]
end
The problem with this is, it only pulls the first school which is the high school. I have a couple of questions here:
If I only want the College, how would I get it to pull University of Illinois at Urbana-Champaign? If there is a high school and college, I would want the code to only pick up whatever school is the college.
Now, let's say I want both the high school and college. How would I pull both and how would I mark that Punahou is the high school and UIUC is the college?
If there are multiple colleges, how would I pull the most recent college entry?
Had a similar question with OmniAuth and LinkedIn, came across your question and it helped me come up with a solution. LinkedIn's hash returns the three current positions in a form similar to Facebook's behavior above. Change [0] to [1] and it will pull the college in your example. Use separate lines for each entry you want. I found the LinkedIn hash returned the three current positions in order with the most recent entry being in the [0] position.
Merchant has_many Shops
Shop belongs_to Merchant
i.e. One merchant (Starbucks) can have many shops locations.
I'm using Gecoder to get the nearby shops, e.g. #shops = Shop.near("Times Square").
I would like to return only 1 record for each merchant only. I.e. #shops only contain 1 Starbucks, 1 Subway, but is a collection.
Sorry I've been Googling and searching on SO to no avail. Perhaps I'm not using the right word to describe what I want. Thanks in advance.
To answer what you should be googling for, joined or combined queries within a scope will probably solve what you are looking to do. If you build a scope with :or logic combining queries, one each for each shop, limited to the first record, you should get what you are looking for.
I won't pretend that I understand Geocoder or advanced scopes enough to do this, but I found an example that shows this approach in another problem:
named_scope :or, lambda { |l, r| {
:conditions =>
"annotations.id IN (#{l.send(:construct_finder_sql,{:select => :id})}) or " +
"annotations.id IN (#{r.send(:construct_finder_sql,{:select => :id})})"
}}
This comes from this SO question: Combine two named scopes with OR (instead of AND)
Hope this helps you find the solution.
I googled a bit more and stumbled on group by for SQL.
If I have 4 shops belonging to 2 merchants near a location called "Raffles Place", within 1 kilometer.
Calling Shop.near("Raffles Place",1) returns 4 shops.
If I add a group to Shop.near("Raffles Place",1).group(:merchant_id), only 2 shops are returned.
This can be used with other conditions too, such as Shop.where(:featured=>true).group(:merchant_id) to only show 1 shop per featured merchant.