activerecord

passing hash object values as search parameter

I have a table "Email" with field "contents" where the data are stored as hash object. I want to do search in this table with parameter "email => [email protected]" where :email is key and "[email protected]" is value of a hash object. Thanks in advance ...

how to modify complex sql query w/ join into rails 3

I'm working on a car pooling application where users can add lifts and are able to select multiple stops for each lift ( A to B via c, d, e). Now when a user searches the database for a lift the results should also include lifts like 'A to d', 'c to B' or 'c to e' and so on. I got this working with Rails 2.3.5 using the below code but st...

Rails: An efficient method to match subdomains in the database?

We allow users to add domains to an active record model such as User.domain and find the users by domain using User.find_by_domain. In the future we want to allow users to enter *.example.com as their domain and allow User.find_by_subdomain('sub1.example.com') and User.find_by_subdomain('sub2.example.com') to work. However we also want ...

Activerecord limit returns NoMethodError

Hi, i'm new to rails. i'm trying to get some results from table in a controller using the limit method. I did that on the controller: @articles = Article.limit(5) And i got NoMethodError exception : undefined method `limit' But when i did it that way it worked: @articles = Article.all(:limit => 5) Although i saw the first metho...

Rails ActiveRecord association "one or the other"

I was wondering, how an association like the following might be done in Rails: class Car < ActiveRecord::Base belongs_to :person end class Truck < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::Base #How to do the association as below? has_one :car or :truck end Essentially, I am trying to enf...

Groovy static generic type

I've been playing around with getting rid of DAOs in favor of ActiveRecord like entities in Java, but generics won't allow me to have the full functionality that I want (like static finders). Somehow Groovy does, but I'm confused why. Given the following: class ActiveRecord<T> { static Class<T> genericType; ActiveRecord() { ...

Atomic deletions in rails

I have an Article which has_many Comments. When i create comments i can use "new" to build them in memory, and the comment records only get created when the article is saved. Does such a mechanism exist for marking comments for deletion, so that their records are only removed when the article is saved? thanks. ...

Migrating from Rails 2 to Rails 3 problem (nil.any?)

I've been upgrading my app from Rails 2.3.8 to Rails 3. After getting all the basic stuff done (configs, routes, and the new AR API, replacing Authlogic with Devise) and getting some mostly static pages to show up fine I get an error in the rest of my site: You have a nil object when you didn't expect it! You might have expected an ins...

Postgres - out of memory on looping insert

Hi all, I'm parsing an XML file with a few thousand elements in it for insertion into a sql database. Everything works fine with sqlite, but postgres dies on me with this error: PGError: ERROR: out of shared memory I was monitoring locks, and it looks like there's a lock for each insert statement, and although I'm just looping over...

Random jokes in a skit (activerecord)

I am working on populating my database with test data using populate.rake: Repertoire.includes(:jokes).each do |r| @jokes = r.jokes Skit.populate 8..12 do |skit| skit.joke_id = @jokes[rand(@jokes.count)].id end end This is giving me a RuntimeError: Called id for nil. How can I populate a skit with random jokes? ...

where does validates method reside in ActiveRecord::Base ?

Hi all, I understand that the documentation to the Rails 3.0.0 validates method is in ActiveModel::Validations::ClassMethods http://www.railsapi.com/doc/rails-v3.0.0/classes/ActiveModel/Validations/ClassMethods.html#M003721 I was wondering then, how a class that inherits from ActiveRecord::Base has the validates method? For example, i...

Active Record Query with Calculated Field

I am trying to use a query with a calculated field in a Yii Relationship definition but all I get is errors. Here is my query: $me = new CDbExpression('CONCAT_WS(\', \', last_name, first_name) AS the_name'); Here is my relation: 'author' => array(self::BELONGS_TO, 'Author', 'auth_id', 'select'=>$me), My problem seems to be that CDbExp...

Creating third level associations where second level belongs_to third on Rails ActiveRecord

Hello all, I have the following model, Purchase, on my Rails app: class Purchase < ActiveRecord::Base [...] belongs_to :payment, :validate => true belongs_to :day, :foreign_key => :day_day, :primary_key => :day, :counter_cache => true [...] end And I have the Day model: class Day < ActiveRecord::Base [...] has_many :...

Error while invoking MySQL Stored procedure(that returns resultset) from Rails 3

I am invoking a stored procedure (MySQL) from my model. This stored procedure returns a resultset, but i get this error... Mysql2::Error: PROCEDURE loqly.sp_venue_nearby_with_questions can't return a result set in the given context:.... Here's the rails code i use - connection.select_all("call sp_some_proc()") I have trie...

counter_cache not decrementing for has_many associations in ActiveReord

My Rails 3 app has 2 models and a third that's join table between them and their has_many relationships. Basically, User and Show are joined by SavedShow, allowing users to save a list of shows: class Show < ActiveRecord::Base has_many :saved_shows has_many :users, :through => :saved_shows end class User < ActiveRecord::Base has_...

Rails, allowing users to create a form that will be used by other users.

Hey Guys, Hope someone can help me out. Currently working on a RoR project that needs a strange kind of functionality. Basically, I have two types of users named User and Researcher. Users can create forms and these forms are stored in the db and filled in by researchers. I have this basic schema create_table "form_fields", :force =...

How can I optimize this situation with ActiveRecord (Rails 3)

Let's say I have the following association: Club has_many users User has_many guns Gun has_many bullets Club: Moe, Larry, Curly Moe: 2 guns gun 1 has 100 bullets gun 2 has 20 bullets Larry: 1 gun gun 1 has 40 bullets Curly: 2 guns gun 1 has 20 bullets gun 2 has 10 bullets Now, I want to find out how many bullets in the CLUB. It'...

How does one create a scope to find "Authors who have zero posts"?

Using Rails 3, this scope works as would be expected: scope :with_posts, lambda { joins(:posts). select("authors.*, count(posts.id) posts_count"). group("posts.author_id"). having("posts_count > 0") } The generated SQL is: SELECT authors.*, count(posts.id) posts_count FROM `authors` INNER J...

Rails belongs_to many models

I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models. Here's the table structure I intend to have: User id Post id user_id #foreign key; a post belongs to a User aka "Who created this post" Comment id u...

Is there easy way to search in fetched records?

So, I want to search in fetched records: p = Product.added_today # Get records by scope # wants something like p.search(:name => 'Clocks') Is there easy (rails way) to do it (gem or something)? ...