activerecord

How to retrieve values from a column called "object_id" in ActiveRecord?

I need to access a legacy relational database via ActiveRecord and that database uses a column named "object_id" as a primary key in a table. Most things work but when I try specify a custom SQL query for an association (see below) ActiveRecord respectively the Ruby interpreter always retrieves the object_id of the Ruby base "Object" ins...

Ruby on Rails get records by highest price

Hey, I have a model Rails model called Orders that has a type_id, location, and price. Each type can have multiple orders at the same location with different prices. Check below for a idea of the table structure. id | type_id | location_id | price ----------------------------------- 1 | 1 | 1 | 12 2 | 1 | 1 ...

ActiveRecord changed? flag doesn't report changes in associated children

I've got the following situation with ActiveRecord (in Rails 2.3.8): class Order < ActiveRecord::Base has_many :documents accepts_nested_attributes_for :documents end class Document <ActiveRecord::Base belongs_to :order end Now, in a controller I want to direct the user differently depending on whether or not they made changes ...

Can authlogic-oid be configured to use a tablename other than session?

I'm working on a rails3 login using Authlogic and Authlogic-oid, with activerecord-oracle_enhanced-adapter, and user/session code pretty much like Holden's demo on github. When I have a session table, I get this error, because there are no quotes around "session": ActiveRecord::StatementInvalid (OCIError: ORA-00903: invalid table name...

Help optimizing ActiveRecord query (voting system)

I have a voting system with two models: Item(id, name) and Vote(id, item_id, user_id). Here's the code I have so far: class Item < ActiveRecord::Base has_many :votes def self.most_popular items = Item.all #where can I optimize here? items.sort {|x,y| x.votes.length <=> y.votes.length}.first #so I don't need to do anything ...

ActiveRecord > MySQL Adapter > Case Sensitivity

I am working with a MySQL database that has capitalized table/field names like Users, Institutions, etc. Because the operating system of the database host is Linux, identifiers (like the table names) are treated as case sensitive. So, failing to capitalize a table name will result in a table does not exist error. The problem I am trying...

Rails - Best-Practice: How to create dependent has_one relations

Could you tell me whats the best practice to create has_one relations? f.e. if i have a user model, and it must have a profile... How could i accomplish that? One solution would be: # user.rb class User << ActiveRecord::Base after_create :set_default_association def set_default_association self.create_profile end end Bu...

what's the difference between belongs_to and has_one?

Hoping someone can clarify the differences between a belongs_to and a has_one. Reading the rails guide hasn't helped me. ...

Storage of different data formats in Rails dynamically defined by admin

I'm facing a problem where I cannot permanently decide which columns one of my models will have. A use case will be this: An admin creates a new dataset, he wants users to answer. In the dataset the admin defines several data points of different format and units. I could imagine the classes to look similar to this: class Dataset < ...

activerecord sum returns a string??

This seems very strange to me, an active record sum returns a string, not a number basket_items.sum("price") This seems to make it work, but i thought i may have missed something, as this seems like very strange behaviour. basket_items.sum("price").to_i ...

Rails: Create association if none is found to avoid nil errors

I have an application where my users can have a set of preferences. Both are stored as ActiveRecord-models as follows: class User < AR::Base has_one :preference_set end class PreferenceSet < AR::Base belongs_to :user end I can now access the preferences of a user: @u = User.first @u.preference_set => #<PreferenceSet...> @u.pre...

ActiveRecord > MySQL Adapter > undefined method `more_results'

I have incorporated ActiveRecord into a script I am writing to process some data in a MySQL database. In my development environment, using Mac OS X, everything works fine, however, when I attempt to deploy the script in a Linux environment, I keep getting the following error: /activerecord-3.0.0/lib/active_record/connection_adapters/mys...

default_scope not working with sessions in my tests

Iam creating an application where a user can have many organisations each organisation has it's own data, things work fine on development, but when I'm in test mode it doesn't work. For example I have a class Tax class Tax < ActiveRecord::Base # This shouln't be neccesary according to documentation before_create :set_organisation_id...

How can I get SQL statement created by ActiveRecord#find without actually executing it?

I am using will_paginate with some complicated queries and it is unable to correctly calculate number of total records (in order to display proper number of page links) - namely because of grouping by multiple columns. So, I am intending to get the SELECT query which would be used to retrieve ALL records, without actually executing it, ...

disconnect to database

Hello there. I'm starting with Castle ActiveRecord. In my application I have a tool create a new data base file (I use SQLite). I create new data base by use ActiveRecordStarter.Initialize() and ActiveRecordStarter.CreateSchema(), then I add some data to this data base. After this I want to disconnect to this database and connect to an...

How should I write this query?

Hi. I'd like to write the following as a MySQL SELECT statement to cut down on the number of queries required to get the information, but I'm not sure how to write it. I have two tables - tags and books_tags (a many-to-many relationship junction table). The final output I want would print as follows: <label for="formFiltertag1"><input ...

Use db:migrate:redo while properly handling exceptions from schema changes

I have a migration that's breaking in the middle of a couple of schema changes. When it breaks an exception is thrown and rake db:migrate exits, leaving my database in a half-migrated state. How can I set it up so that the migration automatically reverts just the changes that have run so far? I'd like to do so globally when in developm...

How to prevent Rails ActiveRecord from logging blob contents?

I'm writing a Rails 2.3.8 application and appreciate the ActiveRecord default logging configuration in development mode which is to show the SQL queries and their values at the server console. But, in the case of blob values I would prefer not seeing them. Is it possible to maintain the same logging level but without getting blobs cont...

has_many :through association results in NameError

I'm trying to do a many-to-many relationship in rails. It's my first try but I'm having a hard time to succeed. I want to be able to do @user.properties or @property.users. #property.rb has_many :ownages, :dependent => :destroy has_many :users, :through => :ownages #user.rb has_many :ownages, :dependent => :destroy has_many :properties...

ActiveRecord Mixing Condition + Better Way to Write This

Is there a better way to write this I have A Condition which previous Look Very clean and easy to understand like Account.first.websites.last.users.find(:all, :conditions => {:country => "XYZ",:status => "Single",:admin => nil) Now the big problem is that user with admin = false is not picked up. i.e I want all the user...