Spree has no access to database from a partial? - ruby-on-rails

I'm sure this has to do with the complexity of Spree's loading.
But my main problem is that Spree can not load a Country. For me it,s Country.find(214). If I check it in my remote console, I find it no worries. All the countries, all the states are there.
But if I try to set Country.find(214) in the controller for states_controller#index, or make a before_load method that does that, or put it in the view itself, it always returns :Error (Couldn't find Country with ID=214).
Crazy, right? I can't think of what to do at this point. If I do Country = Country.first. I can sort of get it to work by just loading an empty template of index.haml. So that means that some sort of country exists that it is tapping into.
Anyone have any theoretical thoughts as to why this is happening? And how I might be able to circumvent it?

Sorry this is a little late, but I stumbled onto your post and figured out what was causing the problem.
Country 214 is USA, and for some reason Spree defaults to that country. Which means that if you don't have USA loaded, you'll hit into that problem.
To get around it, you'll have to manually set up a default country in your initializer:
Spree.config do |config|
Spree::Config.default_country_id = Spree::Country.find_by_name("Singapore").id
end
I hope you've solved this by now though. :)

Its Pretty much easy in Spree2.0.0 should work for every Spree version too.
Spree.config do |config| # Set Country name and Currency like this(Note: you will need to run 'rake db:seed' before this. Change country name in Spree::Country.find_by_name('United Kingdom') replace united kingdom to your desire one)
config.currency = 'EUR'
country = Spree::Country.find_by_name('United Kingdom')
config.default_country_id = country.id if country.present?
# You can also set following options too.
config.site_name = "Teamer Store"
config.override_actionmailer_config = true
config.enable_mail_delivery = true
end

Related

How to get an correspondent id through a model on rails console

I've trying to get the simple situation:
I have a Model called Company, inside Company I have companies and I have the following object: "state_id". I want to find the related companies that contain some id.
This is the only path that I can get the state_id. So, I am doing:
Company.all.each do |comp|
comp.address.city.state_id
end
It works! But when I do:
Company.all.each do |comp|
comp.address.city.state_id = 27
#or comp.address.city.state_id(27)
end
it doesn't return the right data
I know that it simple, but unfortunately, I didn't getting the right output.
Thanks a lot!
Try
Company.joins(address: :city).where(cities: {state_id: 27})
You can make it work with select method. Also, don't mix up == and = - the former one checks equality and the latter one assigns a value:
needed_companies = Company.all.select do |comp|
comp.address.city.state_id == 27
end
But it isn't a correct way, because you're loading all companies in memory and filtering it. Much better is to use SQL query (as in answer by Ursus) and return only needed companies from DB

Using Wikipedia-Client Gem to Update Rails Database

My ruby and Rails is a bit rusty. I have a table in my database called institutes which has some of the columns filled. I want to use the Wikipedia-Client gem to fill some of the others. I want to use the name attribute to find the page on Wikipedia then use page.summary for the description attribute in my table and page.image_urls.first for the picture attribute. At the moment, I'm struggling to work out how I would go about this.
My current code is:
require 'Wikipedia'
Institute.each do |institute|
school = institute.pluck(:name)
page = Wikipedia.find(school)
description = page.summary
picture = page.image_urls.first
Institute.update!(description: description, picture: picture)
end
I'm clearly doing something wrong here to do with the selection and use of the name attribute to find the Wikipedia page, but can't quite work it out. I think even if I were to pluck the name correctly, it wouldn't assign anything to the right id.
If there's also a way to drop the "The" at the beginning of the name in the Wikipedia search if it exists in :name, that would also be helpful as it seems some institutes drop this on Wikipedia.
You can try to use something like this:
#use https://github.com/kenpratt/wikipedia-client
require 'wikipedia'
#select all Institutes through AR model
Institute.all.each do |institute|
#'institute' is an object, so we can get its name by dot operator
school = institute.name
#try to find school as is
#then try to find without 'The'
#and go ahead only if page exists
page = Wikipedia.find(school)
page = Wikipedia.find(school[3..-1].strip) if page.content.nil? and school[0..2].downcase == 'the'
next if page.content.nil?
description = page.summary
picture = page.image_urls.first
#update Institute object
institute.update!(description: description, picture: picture)
end

How to get ISO currency code by country code in rails?

In my rails app I want to use country-code, currency-code, ISO locale code to fetch some data from API. How can I get this information dynamically when user visit my site from anywhere?
I have used geocoder gem so by request.location I will get location's information and using this gem I can get country-code. Now I am not getting how can I get remaining information such as currency-code & ISO locale code?? Can anyone please help me or guide me??
I have seen this money gem but not sure it will provide me all these information.
Thanks in advance :)
I have tried #Prakash Murthy's answer. But there are many issue in this http://www.currency-iso.org/dam/downloads/table_a1.xml I found there is not proper name of all countries and some country has multiple currency_code which made me confused. But finally I found the solution by this single countries gem without creating any database.
Here is how I achieved the solution:
country_name = request.location.data['country_name'] # got country name
c = Country.find_country_by_name(country_name) # got currency details
currency_code = c.currency['code'] # got currency code
Sorry to answer my own question but I have posted here so in future if anyone stuck like me for the same issue then his/her time not wasted.
I found a way that makes it really easy:
Add currency gem to Gemfile, then bundle install
def currency_for_country(currency_iso_code)
ISO3166::Country.new(currency_iso_code).currency_code
end
Then:
currency_for_country('US')
=> "USD"
currency_for_country('AU')
=> "AUD"
This info is based off the countries gem readme
currency-code & ISO locale code are static data which change very rarely - if at all, and are best handled as static information within the system by storing them within the database tables. Might even be a good idea to provide a CRUD interface for managing these data.
One possible source for Currency code : http://www.currency-iso.org/en/home/tables/table-a1.html
List of All Locales and Their Short Codes? has details about getting the list of all locale codes.

Assign image to asset in Spree

If I manually create an image and assign a product. It works great in console:
p = Spree::Product.first
i = Spree::Image.create!(<create info>)
p.images << i
p.save!
When I try to run this in a program. It doesn't do anything. It runs as if the p.images << i never ran.
I tried searching, but didn't know how to search for << very well.
Update
If I add to the program:
p = Spree::Product.first
i = Spree::Image.create!(<create info>)
i = Spree::Image.find(i.id) #this line
p.images << i
p.save!
This works great, but i don't understand what is going on here.
Thanks in advanced!
Justin
So this is pretty common among ORMs.
The collection<< method adds one or more objects to the collection by
setting their foreign keys to the primary key of the calling model.
Per the rails tutorials:
http://guides.rubyonrails.org/association_basics.html
If you go to that page you can do ctrl+f
and search for: <<
That will give you a ton of information on this.
Also I'll recommend one more source. I like the documentation on this ORM a little better:
http://datamapper.org/docs/associations.html
It's NOT active record, but it's very similar. For me, working with datamapper helped me get a better grasp of active record.
Hope that helps.

Why am I losing session when working with a dashed domain name (example-dashed.com)?

I have a website which is www.abrisud.com. This website has 7 domain names (one for each language): abrisud.com, abrisud.it, abrisud.de, etc... and abrisud-enclosure.co.uk.
The problem is on the last one: I am losing my session on every single request. Each Time I load a page I have a different session ID. On the other domains everything is working just fine.
The website is running ruby 1.8.7 and rails 3.0.0.
I am really convinced that the problem comes from the "-" in the domain name but I just can't find anything (or almost anything) on the subject through the web.
Hopefully I am being clear enough, if not just tell me.
Here is the answer :
From Module ActionDispatch::Http::URL (Rails 3.0.x), be sure to read the comments ;-)
# Returns the \domain part of a \host, such as "rubyonrails.org" in "www.rubyonrails.org".
# You can specify a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".
def domain(tld_length = 1)
return nil unless named_host?(host)
host.split('.').last(1 + tld_length).join('.')
end
Well, calling the domain method with the appropriate _tld_lenght_ argument did not play the trick, the request.domain (abrisud-enclosure.co.uk) was good, but not the session_domain (still co.uk).
So I had to add the following lines as a before filter to my application_controller :
def set_session_domain
request.session_options[:domain] = request.domain
end
If you have a better solution I am open to it as I think this is a really dirty fix.
Thanks
I have taken a peak at your site, the cookie is set with: domain=co.uk;path=/
So the problem is within your rails stack and not the browser(s) - time to do some debugging :-)

Resources