activerecord

New model object through an association

I thought it was possible to create a new model object through an association? class Order < ActiveRecord::Base belongs_to :basket end class Basket < ActiveRecord::Base has_one :order end order = Order.new() basket = order.basket.new() # NoMethodError: undefined method `new' for nil:NilClass ...

Importing from one database to another, with confllicting ID fields

I have a Ruby on Rails application that has two active environments, Stage and Production. Our development team has been using Stage, but we would like to move our data onto the production server for various reasons. However, there are conflicting ID's in the new database, so it's not as simple as pulling the data from one location and...

How to reduce the number of queries required to get this result.

Hello, I'm writing an application on top of CodeIgniter to better organize my ebook collection. I'm nearly done, but I realize my 'browse' page is running far too many queries - two per book - to get their information. Obviously not at all ideal, especially since I have about 1000 books to put into this system. I currently have one mo...

ActiveRecord select a string field has a certain length in Rails 3?

I've got a series of Posts and would like to select all posts where its title size is lesser than 30, how to do that? Posts.where("len(title) < 30")? ...

Rails: setting up has_many_through association between tables from different databases

I am trying to setup a has_many :through relationship between two models User and CustomerAccount through another join model AccountOwnership (users and account_ownerships tables are in one db, say db1 and the customer_accounts table is in remote db, say db2). Here is the relevant code, that sets up the associations class User < Activ...

ActiveRecord: Doesnt call after_initialize when method name is passed as a symbol

I noticed that Rails doesn't trigger after_initialize callback when the callback symbol is passed as input. The code below doesn't work. class User < ActiveRecord::Base after_initialize :init_data def init_data puts "In init_data" end end The code below works. class User < ActiveRecord::Base def after_initialize ...

Code igniter MySQL AES with active records?

Hey, How would i use code igniters active records to insert/update/select data from a database using mysql's built in aes encrypt/decrypt functions? I know i could just use the normal sql query, but i'd like to use active records ideally. Thanks ...

What is NHibernate session exactly?

Hi all, It's a long time I'm working with NHibernate session through frameworks like Castle ActiveRecord but never understood what is a session exactly and how should manipulate. Can anybody help? Is there any concise resource? Thanks in Advance ...

Hacking ActiveRecord: add global named scope

I am trying to have a pack of very generic named scopes for ActiveRecord models like this one: module Scopes def self.included(base) base.class_eval do named_scope :not_older_than, lambda {|interval| {:conditions => ["#{table_name}.created_at >= ?", interval.ago] } end end end ActiveRecord::Base.send(:inc...

Why doesn't @event.users.size work?

Trying to get to total number of users for a given event and I'm thinking what I've got should work, but I get the following: Could not find the source association(s) :squads_users in model Squad. Try 'has_many :users, :through => :squads, :source => '. Is it one of :team, :event, :event_division, :users, :point_adjustments, :checkpoi...

update_all through an association

I am trying to use update_all through an association, and i am getting mysql errors, anyone know why please? class Basket < ActiveRecord::Base has_many :basket_items has_many :articles, :through => :basket_items def activate_articles articles.update_all :active => true end end class BasketItem < ActiveRecord::Base belong...

Getting "Unknown attribute error" Anyone help fixing?

(Sorry for long post) Ok guys, so I'm having some issues with something I'm trying, I've been trying to fix it for a long time now, and It's now time to ask for help. Ok, so I have these "grinders", and I want a user to vote for each one, I did two scaffolds: grinder grinder:string posted_timestamp:datetime poster_ip:string votes_up...

Star Schema with Doctrine ORM

Hi, I've just come across the Star Schema for Report-Aggregation. The first Question that came to my mind is whether or not Doctrine ORM supports such denormalized Tables. As I've seen ist, there are a feww kinky behaiviours in dealing with relations, a Model could not completely know. Any ideas? Is it possible? ...

Detail view list from multiple tables(ASP MVC)

I have a page with a list of devices. Every device has a detail page. What i'm trying to achieve is get a list of all projects belonging to a device on the device detail page. A device can have several projects, and a project can also be linked to several devices (many-to-many relation). I have a table in my database to link the device a...

Rails find_by_sql database type dependent

So in rails I use sqlite3 for development and mysql for production. Sqlite3 and mysql handle booleans differently ("t" or "f" in sqlite3 and true or false on mysql). Usually it's not a problem when I do a find because I can do this: Comment.find(:all, :conditions => ["viewed = ?", false]) And rails will insert the appropriate value de...

Is there find_or_create_by_ that takes a hash in Rails?

Here's some of my production code (I had to force line breaks): task = Task.find_or_create_by_username_and_timestamp_and_des \ cription_and_driver_spec_and_driver_spec_origin(username,tim \ estamp,description,driver_spec,driver_spec_origin) Yes, I'm trying to find or create a unique ActiveRecord::Base object. But in current form it's...

How do I count users shirt size?

Event has_many :squads, :dependent => :destroy has_many :users, :through => :squads, :source => :squad_users Squad has_many :squad_users, :dependent => :destroy has_many :users, :through => :squad_users User has_many :squad_users has_many :squads, :through => :squad_users has_many :events, :through => :squads SquadUser belongs_...

Do has_many :through associations work for models that only exist in memory?

for class A < ActiveRecord::Base has_many :bs has_many :cs, :through => :bs end class B < ActiveRecord::Base belongs_to :a belongs_to :c end class C < ActiveRecord::Base has_many :bs end If i bring up a rails console, and do a = A.new b = a.bs.build b.c = C.new Then i get a.cs => [] but a.bs[0].c => c If a is save...

Emails in Rails--how to keep presentation code out of my models.

For my Rails app, we developed an half home-brewed email system. We created a model called Email which then gets added to a queue to be sent (using a web service). To create templates, we've been just mashing strings together in the model, ie: Email < ActiveRecord::Base def self.stock_message(recipient) email = Email.create do...

Server failure: BUG gc_sweep(): unknown data type 0x0

My server keeps crashing. It is especially bad in localized parts of my app where the rest is fine. On one major page it is now happening every time. I've spent over an hour cutting out parts of code, restarting the server and seeing where it fails (i.e. whenever any segment of content is inserted), but my code doesn't seem to be reasona...