activerecord

ActiveRecord gives 'no such column' SQL error for simple has_many association

This is driving me insane! This code used to work fine, but as of a few weeks ago it stopped working, and I can't work out why. Basically a Game has many Patches. The error occurs in my PatchesController, but its reproducible in the rails console like this: first_game = Game.find(:first) first_game.patches As soon as I use the patches...

Rails Custom Functions EDIT: Update from inside the controller.

Right now I have this social media app, where I'm implementing ratings on users per category, What i want to do it's: Whenever an user casts a vote on an article, it will grab all the votes on the article and make an average of the score/votes and insert that value into another model that i have (User Category Rating), now my question i...

SQL: Get a selected row index from a query

Hi everyone! I have an applications that stores players ratings for each tournament. So I have many-to-many association: Tournament has_many :participations, :order => 'rating desc' has_many :players, :through => :participations Participation belongs_to :tournament belongs_to :player Player has_many :participations has_ma...

Getting form data in Yii to a CActiveRecord model works for one model but not for another

I'm getting a submitted form in this way: $resume->attributes = $_POST['ResumeModel']; $profile->attributes = $_POST['UserProfile']; Both CActiveRecord models are correctly populated before this from the corresponding tables, they have the correct data and all. Both models' data is present on $_POST as modified by the form. But it see...

ActiveRecord find through has_one association child attribute

I have models like this: class Discussion < ActiveRecord::Base has_many :comments has_one :special_comment, :class_name => "Comment" end class Comment < ActiveRecord::Base belongs_to :discussion # contains author end How can I select every Discussion through its adjoined :special_comment 'author' association. Effectively I w...

ActiveRecord relations when model has both one and many of the same model

My data resembles this: class Team < ActiveRecord::Base has_many :persons has_one :leader end class Person < ActiveRecord::Base belongs_to :team end Person only belongs to one Team, but of the many team members there is only 1 leader. First question: should I use belongs_to instead of has_one in the Team model? Second: Team i...

rails 2.3.5 - bug makes ActiveRecord::Base.configurations false. How do I track it down?

I've been updating my user test server and now suddenly I got an error every time I invoke rake with anything database-related. Sample error: rake db:drop RAILS_ENV='production' --trace rake aborted! undefined method `[]' for false:FalseClass /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks/databases.rake:96 Here's line 96 of...

Rails date_select helper and validation

I have a date field in a model backed form in my Rails App: <%= f.date_select :birthday, {:start_year => Time.now.year, :end_year => 1900, :use_short_month => true, :order => [:month, :day, :year], :prompt => {:month => 'Month', :day => 'Day', :year => 'Year'}}, {:class => 'year', :id => 'user_birthday'} %> It is being v...

How do you disable ActiveRecord escaping of newlines in text columns?

What causes ActiveRecord to escape newlines in a text column? Is there a way to disable this behavior? (Illustrated below): >> TestModel => TestModelid: integer, value: text, created_at: datetime, updated_at: datetime # Create model instance with newline in text column "value" >> t = TestModel.create!(:value => "\n")...

Performance Issues with ActiveRecord / NHibernate and SQL CE

Hello, Our team is currently working on a WPF Desktop application with ActiveRecord and SQLCE 3.5 for backend. We've been experiencing some strange performance issues when executing queries or creating/updating records. In some cases opening a form which has a few grids and data elements that need to be populated, can take around 2 seco...

Is it possible to know what exactly changed using an observer in ruby on rails?

I need send an email alert when the price of a product changes. Is it possible do this with ActiveRecord::Observer or do I need use programming logic in the edit form? ...

Same model being loaded twice though controller specifies 2 different models

Rails 3... I have a controller.. class OrdersController < ApplicationController def index @os_orders = OrlandoOrder.all @ct_orders = ChicagoOrder.all end end And then I have the two models defined... class ChicagoOrder < Order Order::ActiveRecord::Base.table_name_prefix = 'ct_schema.' end class OrlandoOrder < Order ...

Ruby on Rails - Overriding the association id creation process

Hello there, I'm trying to override the way rails apply and id to an associated object, for example: There are 2 simple models: class Album < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :album end And then I want to do this: album = Album.new :title => 'First Album' album.photos.build alb...

ActiveRecord: user-specific validations

I want to create user-specific validations. User has a column called "rule_values" which is a serialized hash of certain quantities. In a separate model, Foo, I have a validation: class Foo < ActiveRecord::Base belongs_to :user n = self.user.rule_values[:max_awesome_rating] #this line is giving me trouble! validates_presence_o...

Update the value of a field in database by 1 using codeigniter

Hello all I want to implement a SQL statement using codeigniter active record. UPDATE tags SET usage = usage+1 WHERE tag="java"; How can I implement this using Codeigniter active records? Regards ...

How Do I Mix 2 Rails Models Into a Single Find?

I have two models that are not related (in the db sense) but have some common columns (Name, Email, etc.). Class Account < ActiveRecord::Base end Class LegacyAccount < ActiveRecord::Base end For various reasons, I cannot merge these into a single model or do STI. But I would like a simple view that displays all records from both m...

Output all possible SQL statementes in a Rails Application

I have just inherited a medium sized Rails application and I want to try to optimize it right away. I want to start with the database to ensure that indexes are placed where they need to be an so forth. I therefore need to quickly find out all possible SQL statements that ActiveRecord might make so that I can compare that with the databa...

Rails: Non id foreign key lookup ActiveRecord

I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes, this rightly triggers SELE...

Updating a large record set in Rails

Hello all, I need to update a single field across a large set of records. Normally, I would just run a quick SQL update statement from the console and be done with it, but this is a utility that end users need to be able to run in this app. So, here's my code: users = User.find(:all, :select => 'id, flag') users.each do |u| u.flag =...

rails models - two tables have the same primary and foreign key fields

I am using an existing database with a rails app. I can't change the table or column names. Lets say table one is "invoices" and table 2 is "orders" they both have a primary key that is called the same thing, lets say "order_id" invoices can find its order by looking at the primary key "order_id" in the orders table. Vice versa fo...