I'm using Rails 3.2. Here's my code:
transports = %w(car bike)
transports.each do |transport|
#transport = transport.classify.all
end
That code is not working, but I want the results to be:
#cars = Car.all
#bikes = Bike.all
How do I do that?
transports.each do |transport|
instance_variable_set("##{transport}",
transport.classify.constantize.all)
end
Update Given that the entries in the transports array are now singular the correct code to get the result you want is
transports.each do |transport|
instance_variable_set("##{transport.pluralize}",
transport.classify.constantize.all)
end
Related
How can I use Ransack with "find_by_sql"?
I generally do this:
def index
#m = Mymodel.ransack(params[:q])
#mymodels = #m.result.page(params[:page])
end
Can I use Ransack when doing a custom query?:
#mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user])
If I get it correctly, this is want you want:
def index
#m = Mymodel.ransack(params[:q])
#mymodels = #m.result.page(param[:page]).where(user: current_user)
end
To answer your question directly though, you can convert it to an SQL string, but you won't (or would be hard) be able to use current_user as an argument:
def index
#m = Mymodel.ransack(params[:q])
sql = #m.result.page(param[:page]).to_sql
#mymodels = Mymodel.find_by_sql(sql, current_user)
# the line just above won't work immediately because you'll need to
# modify the `sql` variable manually by inserting the `?` that you'll
# need to map that to the `current_user` that you passed as an argument
end
I know how to find single item with given parameter.
#record = Record.find_by(phone: params[:phone])
How do I find all records in db with given parameter?
#records = ?
#records = Record.where(phone: params[:phone])
#records = Record.where(phone: params[:phone]).all
I've managed to scope my posts to just the users I'm following with the following code:
following_ids = current_member.following_members.map(&:id)
#statuses = Status.where(member_id: following_ids).order("created_at DESC")
But I want to also include my own posts and I'm having trouble accomplishing this. So basically combining that code with this code:
#statuses = #member.statuses.order('created_at desc').all
What's the best way to do this.
Pull both arrays of objects and flatten them.
following_ids = current_member.following_members.map(&:id)
#statuses = Status.where(member_id: following_ids).order("created_at DESC")
#statuses << #member.statuses.order('created_at desc').all
#statuses = #statuses.flatten
To see the list in one chronological list, call sort_by
so instead of
#status = #statuses.flatten
use
#status = #statuses.flatten.sort_by{ |status| status.created_at }
I figured this one out I needed to use push not flatten.
#statuses = Status.where("member_id in (?)", following_ids.push(current_member.id)).order("created_at desc").all
I have the following method in a controller:
# GET /units/1
def show
#unit = Unit.find(params[:id]
#product_instances = Array.new
current_user.product_instances.each do |product_instance|
if product_instance.product.unit == #unit
#product_instances.push(product_instance)
end
end
... #rest of method
end
As can be seen, I have four tables/models: User, Product, ProductInstance, and Unit. A User has many ProductInstances. Each ProductInstance maps to a Product. A Unit has many Products.
I would like to fetch only the User's ProductInstances that are linked to a Product in the current Unit. The current code does it, but how can I re-write it better? I'd like to get rid of the for-each loop and if statement and replace it with chained ActiveRecord queries, if possible.
I tried something like below but it didn't work:
#product_instances = current_user.product_instances.where(:product.unit => #unit)
Seems you cannot do :product.unit.
I think you can try this
current_user.product_instances.joins(:product).where("products.unit_id = ?",#unit.id)
or with hashes
current_user.product_instances.joins(:product).where(:products => {:unit_id => #unit.id})
I'm using MongoDB and Mongo Mapper and need to find an embedded document inside of an array. There has to be a simpler way to do this than the way I got working which is:
#obj.subitems.each do |c|
if (c.slug.eql? params[:id])
#subitem = c # this is the variable i need
end
end
Thanks
#subitem = #obj.subitems.detect { |c| c.slug.eql? params[:id] }
http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-detect