This line works:
f = a.select{ |g| g['done']== 'false' }.select{ |g| g['subject'].include? "send" }.pluck('id','done','subject')
But this line
f = a.select{ |g| g['done']== 'false' && g['subject'].include? 'send' }
Does not. I get a
syntax error, unexpected '}', expecting `end'
... g['subject'].include? 'send' }
...
^
What's wrong with the code?
Related
So, I have this
def userposts
posts = Post.where(user_id: params[:id])
postnums = posts.map {|i| i.id }
postsWithInfo = posts.map{|i| {
post: i,
recipe: i.recipe,
recipepic: rails_blob_path(i.recipe.pic) if i.recipe?,
pics: i.pics.map{|p| rails_blob_path(p) }
}}
render json: {posts: postsWithInfo}, status: 200
end
recipe is null if there's no recipe, and that's what I want, but the problem is that recipepic is crashing the thing if i.recipe is null, and that if statement is doing nothing to stop it. Is there a way I can make it so that this works?
The error I get is
/home/dan/code/projects/project5/backend/app/controllers/posts_controller.rb:27: syntax error, unexpected `if' modifier, expecting '}'
...ails_blob_path(i.recipe.pic) if i.recipe?,
... ^~
/home/dan/code/projects/project5/backend/app/controllers/posts_controller.rb:27: syntax error, unexpected ',', expecting '}'
...ath(i.recipe.pic) if i.recipe?,
... ^
/home/dan/code/projects/project5/backend/app/controllers/posts_controller.rb:29: syntax error, unexpected '}', expecting `end'
}}
^
try this way
postsWithInfo = posts.map do |i|
{
post: i,
recipe: i.recipe,
recipepic: i.recipe? ? rails_blob_path(i.recipe.pic) : nil,
pics: i.pics.map{|p| rails_blob_path(p) }
}
end
Let's say, I have the following code:
File.open("text.txt", "w") do |f|
f << "hello"
end
How do I pass the block if it is given as a Proc?
I tried:
log_to_file = Proc.new { |f| f << "hello"}
File.open("text.txt", "w")(&log_to_file)
But this gives me the error:
syntax error, unexpected '(', expecting keyword_end)
Pass it as the last argument
File.open("text.txt", "w", &log_to_file)
If I run, per the docs
a = [:code]
a.collect { |x| x.to_s } # => ["code"]
However if I run
a = [:code]
a.collect({ |x| x.to_s }) # => SyntaxError
As far as I know, ruby has optional parens. How is my syntax getting screwed up here? This is an even bigger problem for me because I want to chain another function after this one, so I require the parens.
You aren't passing the block as a parameter to the parenthesis.
a.collect { |x| x.to_s }
is the same as
a.collect() {|x| x.to_s }
is the same as
a.collect() do |x|
x.to_s
end
And all of that is fairly close to this as well:
block = -> (x) {x.to_s} # Shortcut 'stabby' syntax for lambda{|x| x.to_s}
a.collect(&block) # Or a.collect &block
My problem is that the print t inside the delete_if block prints 'translation missing: en.no key'
This is strange. The error message in the browser shows me my pages parameters.
Here is what is says for tutor_id:
"tutor_id"=>["1", "2"].
I also tried the following inside the block to make sure it was right, and it did indeed return String.
print t.class # => returns 'String'
Also making a call to the following inside the block yields an error
Integer(t) # => yields error: invalid value for Integer(): "translation missing: en.no key"
Likewise, a call to .to_i is not helpful. It always returns 0. Note: this is the behavior o any non-numerical string such as 'hello'.to_s
print t.to_i # always prints '0'
The following is the troublesome code:
#get an array of all tutors' IDs
tutorIds = params[:tutor_id]
tutorIds.delete_if { [t]
print t
Schedule.exists?(["tutor_id = ?", Integer(t)])
}
Update I left out a bit of information so if the delete_if block is
tutorIds.delete_if { [t]
print t
Schedule.exists?(["tutor_id = ?", t ])
}
The error I get is:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "translation missing: en.no key" LINE 1: ...ECT 1 AS one FROM "schedules" WHERE (tutor_id = 'translati... ^ : SELECT 1 AS one FROM "schedules" WHERE (tutor_id = 'translation missing: en.no key') LIMIT 1
Well it was right under my nose. Such a simple mistake. Notice the [] -> ||
tutorIds.delete_if { [t]
print t
Schedule.exists?(["tutor_id = ?", t ])}
Should have been
tutorIds.delete_if { |t|
print t
Schedule.exists?(["tutor_id = ?", t ])}
Can you try
Schedule.where("tutor_id = ?", t.to_i).exists?
instead?
i have this app and while doing a rake db:migrate I get this error
scope :my, lambda { |options = {}|
includes(:permissions).
where("#{quoted_table_name}.user_id = :user OR " <<
"#{quoted_table_name}.assigned_to = :user OR " <<
"permissions.user_id = :user OR " <<
"#{quoted_table_name}.access = 'Public'", :user => options[:user] || User.current_user).
order(options[:order] || "#{quoted_table_name}.id DESC").
limit(options[:limit]) # nil selects all records
}
rake aborted!
/Users/tamer/Sites/fat_free_crm/lib/fat_free_crm/permissions.rb:45: syntax error, unexpected '=', expecting '|'
scope :my, lambda { |options = {}|
^
/Users/tamer/Sites/fat_free_crm/lib/fat_free_crm/permissions.rb:53: syntax error, unexpected '}', expecting kEND
Line 45 is the first line
scope :my, lambda { |options = {}|
Do i need to use ruby 1.9*
No, this wont' work in 1.8.7. Yes, it will work in 1.9.2. The more flexible block arguments were introduced as part of Ruby 1.9, including default arguments.
That said, this scope should really just be pushed off into a class method:
def self.my(options = {})
includes(:permissions).
where("#{quoted_table_name}.user_id = :user OR " <<
"#{quoted_table_name}.assigned_to = :user OR " <<
"permissions.user_id = :user OR " <<
"#{quoted_table_name}.access = 'Public'", :user => options[:user] || User.current_user).
order(options[:order] || "#{quoted_table_name}.id DESC").
limit(options[:limit]) # nil selects all records
end
Same result, only it's 1.8.7 compatible.