spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Dynamically appending .or() calls to an active record query based on array elements


You can use inject to build your query:

ary[1..-1].inject(Object.where(ary[0]),
:or)

This form takes all the elements but the first one, and for each they append .or(ary[i]) to the result, where the initial value is Object.where(ary[0]).

Another way of doing the same thing is using a block:

ary[1..-1].inject(Object.where(ary[0]))
{ |query, cond| query.or(cond) }

Categories : Ruby On Rails

Related to : Dynamically appending .or() calls to an active record query based on array elements
How to get unique records from Active Record rails 4 based on a column
customer.payments.where(:customer_sign_up => 1.year.ago..Date.today).order("created_at.desc").first I've gone for the most recent here with the order field, you can adjust that to whatever you want to use as the criteria for choosing a single payment.

Categories : Ruby On Rails
Codeigniter active record - query 3 tables
Try this $this->db->from("projects p"); $this->db->select("p.id_project,c.categories,c.name"); $this->db->join("projects_categories pc","pc.id_project = p.id_project","LEFT"); $this->db->join("categories c","c.id_category = pc.id_category","LEFT"); $result=$this->db->get()->result_array(); Now $result is your array.

Categories : PHP
validating array objects in active record
You are doing 2 thing at the same time - you use Rails' validates and custom validate. Choose one. In order to use custom validation you have to write the method (what you've done almost Ok): validate :check_weightage def check_weightage errors.add(:weightage, "should be greater than or equal to zero") unless (weightage && weightage >= 0 && weightage.is_a? Integer) end

Categories : Ruby On Rails
Iterate over an Active Record Relation in Rails 3.2
Problem is here: <%= @items.each do |r| %> This line of code iterates over each of the relations and due to the '=' you output its content. Change it to: <% @items.each do |r| %> and you are good to go!

Categories : Ruby On Rails
How to select particular column when using join in active record
Have you tried it like this: $criteria = new CDbCriteria; $criteria->with = 'tagging'; $criteria->select = 'tagging.tag_name'; $criteria->together = true; $criteria->addCondition('t.topic_id = '.$topic->id, 'AND'); $criteria->addCondition('t.version_name = 1', 'AND'); $tags = TopicTagMap::model()->findAll($criteria); You have to define relation in Tagg

Categories : PHP
Recently Add
Groups and Users has many oder has many through?
Rails format specifier differences
Capistrano can not create symlinks: file exist
Admin can't delete users. Ruby on Rails 3 - Michael Hartl
Don't show user name who create post
Using Wicked with Devise (2 step signup process)
RubyOnRails Rake test failures
Rails session start event handler
Why ActiveModel::ForbiddenAttributesError error?
How can i track the issue in production RAILS
Cannot install spree gem - ERROR: invalid gem: package is corrupt
Why are the nested resources for Devise authentication not working?
Rails Devise - how to open access to the site root
Rails: respond_with the same template for two methods
rails with postgresl database "role postgres does not exist"
Instance variable in Rails helper not set
Monkey patch rails 3.2 rake task
Rails_admin: Should I have admin_user or user with admin role to manage users and admin panel
undefined method `attachments' for nil:NilClass
Memory usage increase with Ruby 2.1 versus Ruby 2.0 or 1.9
Can a Rails app be deployed without using Heroku Toolbelt? If so, how?
Enable random access to collection with MongoDB
libmysqlclient-dev installation fails
undefined method `name' for nil:NilClass. Can't find @followed.name but @followed is set
Allowing an arbitrary domain and subdomain in session_store.rb
Pushmeup Gem - Can't send push notifications in Active Admin model
Rails console does not start
GitHub Import: Could not locate Gemfile
AWS::S3::PermanentRedirect in SongsController#index "The bucket you are attempting to access must be addressed using the specified endpoint"
automatically create ActiveRecord parent for association
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.