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
0
votes
1 answer

Initialise model object from table name

Is there a way to instantiate a namespaced model object from its table name? eg: Given table school_students, model School::Student, id 30, I could do: student = get_from_table_name_and_id("school_students", 38)
zsquare
  • 9,916
  • 6
  • 53
  • 87
0
votes
1 answer

What's the ActiveRelation (arel) equivalent to :from

I have this named_scope in one of my models: named_scope :latest_100, :from => '(select * from videos order by videos.created_at desc limit 0, 100) as videos' Its purpose is to create a pool of the last 100 videos (returning that…
0
votes
2 answers

Active Relation: Retrieving records through an association?

I have the following models: class User < ActiveRecord::Base has_many :survey_takings end class SurveyTaking < ActiveRecord::Base belongs_to :survey def self.surveys_taken # must return surveys, not survey_takings where(:state =>…
Jay Levitt
  • 1,680
  • 1
  • 19
  • 28
0
votes
1 answer

Link two models through middlemodel at Rails 3.x

I guess that I do not know the right terminology yet, thus find it difficult to find the right answer. So, I have created an engine with Exhibit and Category. I created a third model Categorization so as to assign an exhibit to more than one…
0
votes
1 answer

Rails :include doesn't include

My models: class Contact < ActiveRecord::Base has_many :addresses has_many :emails has_many :websites accepts_nested_attributes_for :addresses, :emails, :websites attr_accessible :prefix, :first_name, :middle_name, :last_name, :suffix, …
0
votes
2 answers

How to sort forum's users by "total_content_length" and then by most recent post?

I would like to sort forum's users by total_content_length. To get the top n writers in the forum I do: User.order("total_content_length DESC").limit(n) Now, the problem is when there are two (or more) users with the same total_content_length. In…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
0
votes
1 answer

Standard sql subquery to ActiveRecord Relation, is it possible?

I've got a piece of code where I have to applies some subqueries. I'm trying to change this piece of pure sql to more rails way. Is it possible to implement ActiveRecord Relation here? Customer.joins("RIGHT JOIN customers_users ON…
mr_muscle
  • 2,536
  • 18
  • 61
0
votes
1 answer

why is message_id_equals method missing in Rails 3?

I seem to have this error, and I'm not doing anything special: NameError (undefined method `message_id_equals' for class `ActiveRecord::Relation') Why? Here is the context: @user_has_message =…
Satchel
  • 16,414
  • 23
  • 106
  • 192
0
votes
1 answer

DRYing up Active Record Queries

I have 2 queries that are similar, how can I DRY them up? There is only 1 condition that is different between both queries. if self.gender_target == "Both" return Drop.limit(180).live.where( :drops => {:navigation_section_id => 1} …
jdee
  • 11,612
  • 10
  • 38
  • 36
0
votes
1 answer

Using after_create

I have a model, Category. And I want to create an new default sub_category when ever the category is created. But I'm not sure how to do it. Here is what I have. class Category < ActiveRecord::Base attr_accessible :title, :position has_many…
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
0
votes
1 answer

NULL in (NULL) does not match properly

Using Rails 3 active relation, I have a scope: scope :duplicate_contact, lambda {|contact| where( :person_id => contact.person_id, :salutation => contact.salutation, …
DGM
  • 26,629
  • 7
  • 58
  • 79
0
votes
0 answers

How do I model different types of users?

My app will be a database of players. That's the core data point of the database. Players (aka...the profiles of players) are what users will login to view/edit/update/delete. Think of it like a recruiting database, where I (as a recruiter) can…
marcamillion
  • 32,933
  • 55
  • 189
  • 380
0
votes
1 answer

How to combine two ActiveRecord::Relation to form active relation under OR condition?

This link is great but it doesn't address the problem of getting an active_relation after combining two active relation under or condition. There are other links as well but none of them answer the above question. Although for anding method merge…
zeal
  • 465
  • 2
  • 11
  • 22
0
votes
2 answers

sql: DISTINCT give me duplicate results

Hello I am using following query Message.select("DISTINCT(commentable_id, user_id) as owner_id").map(&:owner_id) It gives me the result like this: ["(8,9)", "(8,84)", "(9,8)", "(84,8)"] here "(8, 9)" and "(9, 8)" are returns as different, but I…
sunil
  • 1,040
  • 9
  • 20
0
votes
1 answer

Using ActiveRelation with :joins on polymorphic has_one to implicitly create relation

We can use ActiveRelation like this: MyModel.where(:field => "test").create => # But it doesnt work for joins with polymorphic has_one associations: class RelatedModel < AR::Base # has :some_field belongs_to :subject,…