Cant get ShopifyAPI::Address destroy working - shopify

I have a piece of code in my controller:
customer = ShopifyAPI::Customer.find(custid)
customer.addresses.each do |address|
if address.id.to_s == params[:addressid]
address.destroy
end
end
This returns in the log file response code 406
Ive also tried variations on ShopifyAPI::Address.delete(address.id) but that doesn't work either.
What am I doing wrong?

Ok, answering my own question so other people don't spend hours trying to find an answer. To delete a customer address you need to use activeresource custom methods and do something like below
customer = ShopifyAPI::Customer.find(custid)
customer.delete("addresses/"+params[:addressid])
where params[:addressid] is an address.id you are passing in from a view

Related

Best way to filter unseen videos in Rails

We build a video-section for our users. The user can filter the videos by rating/views/date.
Also the user can decide to hide already seen videos. This is where i struggle a little bit.
right now i have a solution, which is working, but doesnt seem to perform great.
if #filter == "newest"
if #unseen
ids = Videothek::Video.where(videothek_category_id: categories).pluck(:id)
views = Videothek::Video::View.where(user_id: current_user.id).pluck(:video_id)
unseen = ids - views #ids der ungesehenen videos
#videos = Videothek::Video.where(id: unseen).order("created_at DESC")
else
#videos = Videothek::Video.where(videothek_category_id: categories).order("created_at DESC")
end
end
i thought it must be possible to do with a scope, like Videothek::Video.unseen(current_user).order(.....)
A Video has_many Views, but i struggle to get the join running, as i just want the videos, that DONT have an association with an videothek_video_view, where user_id = 1 (or current_user.id).
can somebody help me out?
btw: we are on RoR3
You may use where.not(video_id: [ids]) to make database filter videos user already seen. This method is added since rails 4.
https://robots.thoughtbot.com/activerecords-wherenot
instead of pluck(:id) you may use .ids. I would also move the code somewhere out of controller.
Probably you question would fit better to https://codereview.stackexchange.com/ since you already have working version.

Locate database entry based on ID from another database entry in rails

I've been digging around a little trying to figure out how I should locate the "tweet_id" in my #savedtweets table and then locate that same "tweet_id" in my #newtweets table from a controller, so far I'ved tried something like this;
CONTROLLER
#stweet = Savedtweet.find(params[:id])
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
#newtweet.status = 'new'
#newtweet.save
Basically I need to change the string "saved" in my Newtweets table to "new" based on the current Savedtweet ID. I just can't figure it out. If I do the following in console;
#stweet = Savedtweet.first
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
It finds the right one. I've got to be close just not there yet. :)
You could do:
Newtweet.find_by_tweet_id(#stweet.tweet_id).update_attribute(:status, 'new')
The reason your code isn't working is because Newtweet.where() returns an array of objects. It should be Newtweet.where().first, though Newtweet.find_by_tweet_id is the preferred method.

Rails 3 - Record.send not working

I have a view that shows all of my customers. I have a Customer model that is backing this view. There is a method in the model that will change an attribute from "" to a persons initials, "jh". Here is the method.
def print(item, attribute)
value = item.send(attribute)
if(value.blank?)
item.send(attribute + '=', "jh")
else
item.send(attribute + '=', "")
end
end
When I run this code from the console, it works perfectly. When I try it in the app, nothing changes. I am getting the proper 'value' that I expect, but it is never turned into an empty string. I am sure I am missing something simple here, but please help. Thanks.
I don't see any problem with this code, and I tried it on both a plain ruby object and on an ActiveRecord model and both worked as expected. So I suspect something funny is happening that is specific to your code.
I would suggest in any case that rather than construct a setter via string concatenation, you should use Ruby's native instance_variable_set:
def print(item, attribute)
value = item.send(attribute)
if(value.blank?)
item.instance_variable_set("##{attribute}", "jh")
else
item.instance_variable_set("##{attribute}", "")
end
end
One caveat with this method is that it will create an instance variable if none previously existed.

Rails 3: Building a set of filters on an index view using scopes

I have an index view of a model which I would like to filter by some combination of the model's attributes.
For example, I have a Bill model (not the kind on ducks, the kind you have to pay) that I might filter on payee and/or status.
The model has a scope for each individual attribute, e.g.
scope :bill_status, lambda {|status| where("status = ?", status}
scope :bill_payee, lambda {|payee| where("payee_id = ?", payee.id}
The view allows the user to select zero or more options -- if an option is not selected, it means "don't filter by this".
In the controller, I can do something yucky like this:
def index
status = params[:bill][:status]
payee = params[:bill][:payee]
if status.present? and payee.present?
# chain scopes
#bills = Bill.bill_status(status).bill_payee(payee)
elsif status.present?
#bills = Bill.bill_status(status)
elsif payee.present?
#bills = Bill.bill_payee(payee)
else
#bills = Bill.all
end
# rest of controller action
end
But while this works, it's neither pretty nor easily extensible -- adding a third filter means I now have many more possibilities. I seek beauty and purity.
On the assumption that my scopes are all chainable, it seems like I should be able to do something like
def index
#bills = Bill.all
#bills = #bills.bill_status(params[:bill][:status]) if params[:bill][:status].present?
#bills = #bills.bill_payee(params[:bill][:payee]) if params[:bill][:payee].present?
# rest of controller code
end
'cept it doesn't work because Bill.all is an array. Plus, that's no fun because Bill.all executes the query, which I only want to run once thanks to AREL magic. Should I just define a scope like all_bills (with no conditions?) -- that would be an ActiveRecord::Relation I guess...
Is there a pattern that solves this problem more elegantly? Whenever I have code to do one thing that relies on Model, View and Controller I feel as though I might be doing something wrong. Or as though someone smarter and harder working than I has already solved it :-)
Bonus question: I want this all to work with my paginator of choice, the most excellent Kaminari gem.
All thoughts and ideas welcomed.
I'd do something like this:
proxy = Bill.scoped
if status.present?
proxy = proxy.bill_status(status)
end
if payee.present?
proxy = proxy.bill_payee(payee)
end
#bills = proxy
You can even do then some meta-programming:
#bills = [:status, :payee, ...].inject(Bill.scoped) do |proxy, param|
val = params[:bill][param]
val.present ? proxy.send("bill_#{param}", val) : proxy
end
As I searched for solutions to what seemed like a common problem, I checked out Ryan Bates' RailsCast and found an episode from 3 days ago on Ransack. Ransack is a pretty seriously cool gem for form searching and column sorting, and I think that's the way I am going.
Thanks for the answers here -- I am glad to have learned a couple of great techniques from those who took the time and effort the answer.

Problem with Thinking Sphinx and Functional Tests

this is my test (with shoulda helpers):
context "searching from header" do
setup do
Factory(:city, :name => 'Testing It')
ThinkingSphinx::Test.index 'city_core', 'city_delta'
ThinkingSphinx::Test.start
get :index,
:query => 'Testing It'
end
should respond_with(:success)
should assign_to(:results)
should "have one city on the result" do
assert_equal( assigns(:results).count, 1 )
assert_kind_of( assigns(:results).first, City )
end
ThinkingSphinx::Test.stop
end
Everything works fine except the test always say the count of the results is 0, not 1.
I have debugged this code and when the request reaches the controller, the Sphinx indexes are completely empty, even with the explicit call of index for it.
Am I doing something wrong here?
Any help appreciated.
I found out the problem... even tho the insertion in the database is right before the ThinkingSphinx.index, with transactional fixtures, after the setup block the records get deleted.
The solution was adding to the test the following line:
self.use_transactional_fixtures = false
Hope this helps anyone with the same problem.