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
6
votes
4 answers

Matching nested model association attribute with includes

Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, name. I want to search for all posts with a given author…
5
votes
2 answers

rails 3.0.3 - ActiveRecord::Relation, undefined method error

I am having this unexplained ActiveRecord::Relation, undefined method error . I don't know why, since my model association are well defined and the event table has the foreign keys for the user table. I tried using this fix but it failed: Rails 3…
brg
  • 3,915
  • 8
  • 37
  • 66
5
votes
3 answers

Rails 3 query in multiple date ranges

Suppose we have some date ranges, for example: ranges = [ [(12.months.ago)..(8.months.ago)], [(7.months.ago)..(6.months.ago)], [(5.months.ago)..(4.months.ago)], [(3.months.ago)..(2.months.ago)], …
Israel
  • 3,252
  • 4
  • 36
  • 54
4
votes
3 answers

Rails ActiveRelation to hash

I need to hashify ActiveRelation! For example I have such AR arel = MyClass.page(options[:page]).per(options[:per]) Is here a way simple to create hash with primary keys of MyTable as keys in hash Ex1: [#, #
Fivell
  • 11,829
  • 3
  • 61
  • 99
3
votes
2 answers

has_many association with select

all! I want to create additional has_many relation to select only needed columns Example class Price < ActiveRecord::Base self.table_name = "pricelist_prices" has_many :order_items, :primary_key=> :city_id, :foreign_key=> :city_id has_many…
Fivell
  • 11,829
  • 3
  • 61
  • 99
3
votes
1 answer

Ruby on Rails - How to join two tables?

I have two tables (subjects and pages) in one-to-many relations. I want to add criterias from subjects as well pages to parse a sql, but the progress has been very slow and often times running into problems. I'm brand new in rails, please…
jmsia
  • 43
  • 1
  • 1
  • 4
3
votes
2 answers

Rails - Is there any way to set an overrideable "default" filter (i.e. pre-run class method) for a model's ActiveRecord::Relation queries?

I have a group of records I'm pushing into a database from another format. According to my records' supposedly unique ids, some of the records I've push into my database are getting rolled back because it's saying they're duplicates. But I checked…
2
votes
1 answer

How to name the has_many thru join model between User and Blog?

I'm not a native english speaker, but if you are, how would you name this Rails ActiveRecord join model "???" in a semantic fashion? User has_many :blogs, through: ??? User has_many ??? ??? belongs_to :user ??? belongs_to :blog Blog has_many…
svoop
  • 3,318
  • 1
  • 23
  • 41
2
votes
1 answer

How do I select an object and a count together in Rails 3 Active Record Query?

How do I do something like this in active query? select product.*, count(product_id) AS product_counts group by(product.*) order by product_counts; I tried this in active query: Product.select("product fields, count(product_id) AS…
2
votes
2 answers

Exclude conditions in active relations, how?

I am seeking a way to do the opposite of : Model.where(:name => 'julian') Something like : Model.where(:name => is_not('julian')) I have found this post. But, find(:all, :conditions => {}) is now deprecated in rails 3 and moreover, I think a…
Hartator
  • 5,029
  • 4
  • 43
  • 73
2
votes
2 answers

Why/How is a class method accessible to an array of records only through an association in Rails 3?

Consider a simple example, where we have 2 models, Article and Category. class Article < ActiveRecord::Base belongs_to :category def self.search(title) where(:title => title) end end class Category < ActiveRecord::Base has_many…
Shreyas
  • 8,737
  • 7
  • 44
  • 56
2
votes
2 answers

Rails3 & ActiveRecord::Relation - Arel and OR

I have some scopes definied in a Model called Events. Now I need join them using OR operator. I’m using Rails3 and I’m a bit confused with ActiveRelation & Arel and how and how I should use this scope :issues, where(:subject_type => …
2
votes
1 answer

Saving a collection of objects rails 4 (strong params)

I am currently working on the backend of shop. The Client wants to be able to see a list of all products and update the stock values for all the products in one submission of a form. I have a working solution, but it's a very 'hacky' and introduces…
2
votes
0 answers

Is there way to check if given SQL was executed in Rails

I was wondering, is there easy way how to check if a given SQL was executed in current request? let say I have class Candy < ActiveRecord::Base def yummiest_candies @yummiest_candies = where(yummy: true, user_id: 1) end def…
equivalent8
  • 13,754
  • 8
  • 81
  • 109
2
votes
0 answers

How do I remove the 'table_name.%' prefixes from ActiveRecord queries?

Background: I am writing an ActiveRecord adapter for Cassandra, specifically one that builds on the JDBC driver (activerecord-jdbccassandra-adapter). Problem: CQL resembles SQL, but it has some notable differences. The main one I am running up…
plainprogrammer
  • 584
  • 3
  • 12