I am trying to create a query to record my submissions for this month in my controller using the following code:
#today = Time.now
#thismonth = #today.month
#nextmonth = #thismonth + 1
#submissions = SubmissionLocation.includes(:submission).where(location_id: current_agent.Company_Business_Location, location_id: 2134).map(&:submission)
#submissions_by_group = #submissions.group_by { |m| m.Max_move_in_date.beginning_of_month}.sort_by{:Max_move_in_date}
#submission_this_month = #submissions_by_group.where(:Max_move_in_date => #thismonth)
It seems to be throwing a nomethod error for the where condition in the #submission_this_month instance variable. I understand that it is most likely because my data is now in a hash format, so in that case, how I would I then query it?
Related
I am working on a multistep form for an order placement section which uses a session session[:order_params] to store all form inputs before submit.
I need to be able to access a particular parameter (land) from the session in order to query for another resource (shippingservice) when navigating back in the form.
In my orders_controller.rb I have:
#shippingservices = #cart.available_shipping_services.joins(:lands).where(:lands => {:id => params[:id]})
but would need to specify the land.id from the session[:order_params].
When using session[:order_params] I get ActiveRecord::StatementInvalid in Orders#create:
Mysql::Error: Unknown column 'id.ship_to_last_name' in 'where clause': SELECT `shippingservices`.* FROM `shippingservices`
INNER JOIN `zones` ON `zones`.`id` = `shippingservices`.`zone_id`
INNER JOIN `lands_zones` ON `lands_zones`.`zone_id` = `zones`.`id`
INNER JOIN `lands` ON `lands`.`id` = `lands_zones`.`land_id`
WHERE `id`.`ship_to_last_name` = 'Smith'
AND `id`.`ship_to_address` = 'Somewherestreet'
AND `id`.`ship_to_city` = 'Nowheretown'
AND `id`.`ship_to_postal_code` = '99999'
AND `id`.`phone_number` = 'some number'
AND `id`.`shippingservice_id` = '34'
AND `id`.`email` = 'someone#example.tld'
AND `id`.`land_id` = '85'
AND `id`.`ship_to_first_name` = 'John'
AND (weightmin <= 200 AND weightmax >= 200 AND heightmin <= 12 AND heightmax >= 12 AND shippingservices.shippingcarrier = '1') AND (lengthmax >= 210 AND widthmax >= 149)
Since the correct land_id is present I am wondering how to provide only that value to the query.
Thank you in advance!
As per the description mentioned in the post, you want to access a particular key stored in session at a particular key.
Assuming order_params is a hash, you can get land_id using the below mentioned code:
session[:order_params][:land_id]
This will return the value of land_id and thus you can use it in the query.
To set session variable, you can set some data in a controller action
For eg:
app/controllers/sessions_controller.rb
def create
# ...
session[:current_user_id] = #user.id
# ...
end
And read it in another: app/controllers/users_controller.rb
def index
current_user = User.find_by_id(session[:current_user_id])
# ...
end
I'm pretty new to Ruby scripting and have made a script to loop through product collections in Shopify to change the 'product match condition' on all collections but I can't seem to get it to work at all unless I specify the collection ID.
I keep getting an error saying "NoMethodError: undefined method `disjunctive=' for #" and I can't figure out why. The script I've written is below:
Failed collection loop:
count = ShopifyAPI::SmartCollection.count
page = 1
while count > 0
collectionsEdits = ShopifyAPI::SmartCollection.find(:all,:params => {:page=> page})
collectionsEdits.disjunctive = 'true'
collectionsEdits.save
count = count - 50
page = page + 1
end
Specific collection ID:
collectionUpdate = ShopifyAPI::SmartCollection.find(437592964)
collectionUpdate.disjunctive = 'true'
collectionUpdate.save
Any help would be greatly appreciated!
API Documentation: https://help.shopify.com/api/reference/smartcollection
if Shopify::SmartCollection is just an ActiveRecord model so I'd recommend you to write that like this:
ShopifyAPI::SmartCollection.where(page: page).update_all(disjunctive: 'true')
P.S. in where clause you could provide any hash with your conditions
In your case error occurs because find(:all) return a collection but not a single instance that's why you cannot execute disjunctive = 'true'
So if you still want to write that your way do it like this:
count = ShopifyAPI::SmartCollection.count
page = 1
while count > 0
collectionsEdits = ShopifyAPI::SmartCollection.find(:all,:params => {:page=> page})
collectionEdits.each do |collectionEdit|
collectionEdit.disjunctive = 'true'
collectionEdit.save
end
count = count - 50
page = page + 1
end
So, Ive been trying to query my PostgreSQL model in Rails, but get the following error:
undefined method `id' for TransactionTemplate::ActiveRecord_Relation
My code:
#transaction_templates = TransactionTemplate.where("transaction_category_id = 1")
#transaction = Transaction.where("transaction_template_id in (?)", #transaction_templates.id)
I know that the transaction templates is an array and that there is therefor not just one ID it needs to look up, but multiple of IDs, just as I want it.
Any suggestions?
Try this:
#transaction_templates = TransactionTemplate.where("transaction_category_id = 1")
#transaction = Transaction.where("transaction_template_id in (?)", #transaction_templates.map(&:id))
If you need just ids then try:
#transaction_template_ids = TransactionTemplate.where("transaction_category_id = 1").pluck(:id)
#transaction = Transaction.where("transaction_template_id in (?)", #transaction_template_ids)
Or you have proper associations then
#transaction = Transaction.joins(:transaction_template).where("transaction_category_id = 1")
I'd like to ask a little question. It's rather trivial I guess but I might just look for the wrong solutions to my issue so I cant get it working.
I have a model called request.rb which has a method called self.dates
def self.dates
from_date = Request.last.date.to_date
to_date = Request.last.todate.to_date
weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
end
I have another model called hotels.rb where I'd like to call the variables weekdays and weekend_days for the price calculation.
def self.best_deal_nm
weekday_nm_tot = (Request.dates.weekdays * pricea.wdpricenm) + (Request.dates.weekdays * pricea.wepricenm)
weekend_nm_tot = (Request.dates.weekend_days * priceb.wdpricenm) + (Request.dates.weekend_days * priceb.wepricenm)
[weekday_nm_tot, weekend_nm_tot].min
end
Unfortunately the code above doesnt work. My question is therefore how can I possibly call these two variables in my hotel model.
Thanks a lot for help
Just return all info in last line into your self.dates method like
def self.dates
from_date = Request.last.date.to_date
to_date = Request.last.todate.to_date
weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
{'from_date' => from_date, 'to_date' => to_date, 'weekdays' => weekdays, 'weekend_days' => weekend_days}
end
After call Request.dates from hotels.rb you could access to all variables added to hash.
weekday_nm_tot = (Request.dates['weekdays'] * pricea.wdpricenm) + (Request.dates['weekdays'] * pricea.wepricenm)
I have the following action in my controller:
def find_by_registration_date
#registration_date = params[:registration_date]
#registrations = Registration.where(:created_at => #registration_date)
end
...were params[:registration_date] is a simple date (no time) like:
"registration_date"=>"2014-07-16"
...and my created_at date looks like...
created_at: "2014-07-16 15:50:52"
How can a search based on Y-M-D?
If its mysql, you can do
#registrations = Registration.where("DATE(created_at) = ? ", #registration_date)
For other databases, find the equivalent date function