Questions tagged [active-relation]

A Ruby Relational Algebra Library

Active Relation also known as "Arel" is the Relational Algebra library used in Active Record and Ruby on Rails.

69 questions
2
votes
2 answers

Chain meta_search results

How can I chain metasearchs' search method ? @result = User.search(params[:search]).search(params[:filters]) We can call chain method on ActiveRecord like User.active.male.where( age: 14..20) its chaining is possible on meta search's result ?
1
vote
2 answers

Rails: How to sort messages with comments?

Message can have multiple comments: class Message < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :message end The following named scope returns messages that were created in a given range of time,…
1
vote
1 answer

Rails3 confused on active relations

I have checkedout the docs on this but I am still a bit confused. My goal is to return the content field on @mom. But it fails with undefined method `content'. and @goals works. What am I missing about @mom and how can I get that to…
jlarry
  • 430
  • 5
  • 18
1
vote
1 answer

Rails 3 ActiveRelation adding "is null" on a join... how do I stop it from doing that?

I am trying my first join and the sql that it's generating is very odd. I have a Recipient belongs to a User. I am trying to query all the recipients by a user that are also not read and not deleted: scope :unread, where(:is_read =>…
egervari
  • 22,372
  • 32
  • 121
  • 175
1
vote
2 answers

How can I search through a has_many association in Rails 3 (meta_where or railslogic?)

I have a model called UserHasMessages where: belongs_to :message belongs_to :user And User.rb model is: has_many :messages, :through => :user_has_messages I want to find Users where the associated UserHasMessages has a Message_id of @message.id I…
Satchel
  • 16,414
  • 23
  • 106
  • 192
1
vote
1 answer

User.where(:id => [1,2]).where(:id => 2).arel.to_sql unexpected result

From rails console, running the following gives an unexpected result: User.where(:id => [1,2]).where(:id => 2).arel.to_sql => "SELECT `users`.* FROM `users` WHERE (`users`.`id` IN (1, 2) OR `users`.`id` = 2)" I'm not expecting that to be an…
jacob
  • 31
  • 1
  • 6
1
vote
1 answer

WITH Queries by ActiveRecord::QueryMethods And Return ActiveRecord_Relation rails

I'm using select in with query by find_by_sql: scope :error_percentage, -> (user_obj) { find_by_sql("WITH products_boost_sum AS (SELECT *, (CASE WHEN (city_id =#{user_obj.user.city_id || -1}) THEN 1 ELSE 0 END) + (CASE WHEN (country_id…
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45
1
vote
1 answer

How to query .where on array of clients?

Using Rails 5. I have an ordered array of clients. It's not possible for me to get an AR out of it, and this is the only way I can get these clients. And I want to show only the clients where a certain condition is met. .where(category_id: 1), but…
1
vote
2 answers

Rails: Getting all has_many objects associated with an active relation of parents

I have what I suspect is a basic question. In my rails app, One user has many scores. Is there a way to get all the scores of an active relation of users. I can do user.scores (obviously) but if users is a group of users I need to do something like…
GhostRider
  • 2,109
  • 7
  • 35
  • 53
1
vote
2 answers

Querying model by attribute of HABTM association

I have an HABTM association between two models, User and Conversation. I want to be able to query the logged-in user's conversations (current_user.conversations), passing it another user's id, and have it return a conversation that is shared by…
1
vote
2 answers

rails where for checking multiple condition

Is there any short way to right following query in rails: Message.where("(user_id = 8 AND commentable_id= 84) OR (user_id=84 AND commentable_id=8) ") Thanks
sunil
  • 1,040
  • 9
  • 20
1
vote
2 answers

Ruby on Rails error in active relation, NoMethodError: undefined method 'methodname' for #

Here is code of two models which have 1 to 1 association class User < ActiveRecord::Base has_one :e_user validates_presence_of :first_name validates_presence_of :last_name validates :password, presence: true, :length => { :minimum => 6} …
user2603796
1
vote
1 answer

Trying to return AREL not Array in Rails

I have two models: Products and Tags through a products_tags join in a HABTM relationship. I am currently defining my controller index as: @stats = Product.all(:include => :tags).uniq which returns an array. How can I return an Active Rel object? I…
1
vote
1 answer

Postgres adapter appears to return nil dates for columns that actually have non-NULL values

Postgres 9.0.4 Rails 3.0.7 AR 3.0.7 pg 0.12.2 I have a table with 3 records in it with DATE column values of '0001-01-01 BC' (not sure how they got in there in the first place, there is no default value set on the column). When I request these…
Wes Gamble
  • 777
  • 10
  • 29
0
votes
2 answers

Object.destroy generates ArgumentError: wrong number of arguments (1 for 0)

I've a simple model where class User < ActiveRecord::Base has_many :comments, dependent: :destroy has_many :answers, dependent: :destroy end When i try to delete whith User.first.destroy (on console) i got: ArgumentError: wrong number of…
matreyes
  • 18
  • 1
  • 6