I'm trying to get these statements to work:
#all_ratings = ["G","PG","PG-13","R"]
#valid_ratings = params["ratings"]
#movies = Movie.find(:all , :conditions => {#valid_ratings[:rating.upcase] => "1"} )
but I am getting the error:
undefined method `to_sym' for nil:NilClass
when I should be getting a match.
An example input is:
"ratings"=>{"PG-13"=>"1"}
Where am I going wrong?
More info:
The table has three fields, the title, release date, and rating, and is very simple. The options for rating are stated above in #all_ratings.
Rails 3.x:
#all_ratings = ["G","PG","PG-13","R"]
#valid_ratings = params["ratings"]
# Is #valid_ratings the same as you example, "ratings"=>{"PG-13"=>"1"}?
# It would be easiest to pass a subset of #all_ratings such that the params
# get converted to something like this: "ratings"=>["G", "PG"]
Movie.where(:rating => valid_ratings).all
# SQL: SELECT * FROM movies WHERE rating IN ('G','PG')
I am not sure what you are trying to with :rating.upcase. Is there a variable named rating? :rating is a symbol. upcase is not a method on Symbol.
It tells you that you #valid_ratings in nil
You probably trying doing this?
#valid_ratings = Rating.find(params["ratings"])
Related
I want to call the ActiveRecord method where with an array for a column. If the each item on the array doesn't exist, create the object. The closest method I found for this is first_or_create but this seems to be called only once, not for each time the record doesn't exist. Below is my example code-
hashtag_list = params[:message][:hashtag_primary]
#hashtags = Hashtag.where({:name => hashtag_list}).first_or_create do |hashtag|
hashtag.creator = current_user.id
end
Rails version- 4.2.1
I don't know a direct method, only a workaround
existing_tags = Hashtag.where({:name => hashtag_list}).pluck(:name)
not_existing_tags = hashtag_list - existing_tags
#hashtags = Hashtag.where({:name => existing_tags}).all
not_existing_tags.each do |tag|
#hashtags << Hashtag.new name: tag
end
#hashtags.each do |hashtag|
hashtag.creator = current_user.id
end
This is expected behavior of where + first_or_create method. Basically where(field: array) produces an SQL to find all records where field matches any item in the array. Than you have first_or_create method which takes the first record from results or creates a new one with escaped array value assigned to a field (so something like field: "[\"foo\", \"bar\"]" when used as where(field: %w(foo bar)).
If you want to create records for each hashtag from your list, you should iterate over it:
if #hashtag = Hashtag.where({:name => hashtag_list}).first
# do something if found the first one
else
hashtag_list.each do |hashtag|
# create an object
end
end
If you want to create missing hashtags even if the record is found, you can extract this to a private helper method with missing tags as the argument and re-write code as:
if #hashtags = Hashtag.where({:name => hashtag_list})
# do something if found
end
create_missing_hashtags(hashtag_list - #hashtags.pluck(:name))
I've been reading Checking for nil in view in Ruby on Rails but I'm struggling to implement the marked solution.
I want to only load a graph in my View if a result set is not nil.
Controller:
#statistics = # ...my ActiveRecord query...
Helper:
def show_stats(statistics)
if statistics.pluck(:count)
image_tag(Gchart.line :size => '640x260',
:stacked => false,
:title => '',
:data => [statistics.pluck(:count)],
:legend => ['Stats'],
:bar_colors => ['3300CC', '3399FF'],
:axis_with_labels => ['y'])
end
end
View (HAML):
= show_stats(#statistics)
Currently when there are no statistics, I get an error. I want the View to not render the graph if there are no statistics. What am I doing wrong in the helper?
Error:
undefined method `-' for nil:NilClass
on the line where I call the helper.
if i understand correctly statistics.pluck(:count) will always return an array consisting of values of count attribute for each record found.
in ruby empty array evaluates to true, you might try to rewrite that if line like this:
if statistics.pluck(:count).any?
in fact it's good idea to cache that value and not fetch it from db again few lines below:
if (counts = statistics.pluck(:count)).any?
...
:data => [counts]
...
end
also i assume :data option wants array of values and not array of array of values so the final version would be:
if (counts = statistics.pluck(:count)).any?
...
:data => counts
...
end
P.S. if you still have an error - please share a full backtrace with us, knowing only "undefined method" doesn't tell much
Why not check for #statistics in your view like follows:
= show_stats(#statistics) if #statistics
Did you try this?
= show_stats(#statistics) unless #statistics.nil?
How get items from table ? I want get value from question column, using condition.
#result = Customers.where(:name => session[:username], :email => session[:useremail])
Now, I can get value from any column ? like this: #result.column_from_customers_table , right ?
This is a common mistake for beginners. The code you have returns an ActiveRecord::Relation object and doesn't actually connect to your db yet. In order to get a record you have to loop through each one of the results or call .first on it in order to get the first matching result
# returns an ActiveRecord::Relation object
#results = Customers.where(:name => session[:username], :email => session[:useremail])
# returns the first matching record
#object = #results.first
# then you can call the column names on #object
#object.name
#object.email
# looping through the results
#results.each do |object|
puts object.name
puts object.email
end
In SQL I would do this:
SELECT minimummonths WHERE name = "gold"
I want to do the same in Ruby on Rails and have the following in the new section of my orders controller:
#plan = params[:plan]
#Payplanrow = Payplan.where(:name => #plan).minimummonths
I then try to display #payplanrow in my page using <%=#Payplanrow %> but it doesnt work. I get the error:
undefined method `minimummonths' for #<ActiveRecord::Relation:0x007fe30f870ec0>
I want to print the minimummonths value for the plan selected. There will only ever be one row of data corresponding to the #plan value.
I'm pretty new to Ruby on Rails so I'm just trying to get a pointer in the right direction. I looked everywhere but there doesn't seem to be an example of this.
The problem is Payplan.where(:name => #plan) is returning an array of Payplan objects. Assuming you are using Rails 3, you can read more about it in "Active Record Query Interface".
But, if you are certain that your query is returning only one record you could do:
#Payplanrow = Payplan.where(:name => #plan).first.try(:minimummonths)
The Rails way is to have a scope in your model:
class Payplan < ActiveRecord::Base
scope :by_name, lambda {|name|
{:conditions => {:name => name}}
}
end
#controller
#Payplanrow = Payplan.by_name(#plan).first.try(:minimummonths)
Although it's not really optimal, you can do:
#Payplanrow = Payplan.where(:name => #plan).first.minimummonths
You can use pluck to get only the minimummonths value :
minimummonths = Payplan.where(:name => #plan).pluck(:minimummonths).first
Instead of using where then first, it's better to use find when you are expecting a single record.
#Payplanrow = Payplan.find_by_name(#plan).try(:minimummonths)
That should be:
Payplan.where(:name => #plan).first.minimummonths
Undefined method errors when I try to use a column alias for an aggregate (PostgreSQL)
Inside my model:
class B2bLoginAttempt < ActiveRecord::Base
set_table_name "b2b_logins"
end
Inside my controller:
#client_ip = request.env['REMOTE_ADDR']
#sql = "select count(id) as failed_logins FROM b2b_logins WHERE ip_address = '"+#client_ip+"'"
f = B2bLoginAttempt.find_by_sql(#sql)
failed_attempts = f.failed_logins.to_s
f.destroy
Then I see: undefined method `failed_logins' for #<Array:0x104d08478>
The error occurs because find_by_sql returns an array, so you need to write f.first.failed_logins.to_s instead of f.failed_logins.to_s.
Check find_by_sql doc here.
Not sure I am completely following your logic correctly, but it seems like you may be better off with this:
f = B2bLoginAttempt.where("ip_address = ?", #client_ip)
f.map(&:destroy)
You can get the actual count with f.count
Did I miss something?