activerecord

How to make activerecord use a field generated on the fly by find_by_sql

I'm using find_by_sql with Activerecord which I generate another field there that doesn't in the original table as a combination of different fields like: select (field1 + field2) as new_field_name If I try to access the newly generated field like: @user.new_field_name I get nothing! How do you suggest I should approach this proble...

How do I create a dynamic method in PHP?

I'm trying to extend my ActiveRecord class with some dynamic methods. I would like to be able to run this from my controller $user = User::find_by_username(param); $user = User::find_by_email(param); I've read a little about overloading and think that's the key. I'v got a static $_attributes in my AR class and I get the table name by ...

How do I create a new instance of a different class when saving a model

I have a Class A that when it is instantiated and saved for the first time or modified it will create an instance of Class B and save it also I want them to be in the same transaction and I want it to be handled in the Model not a controller. I know how to do this in a conteoller with the ‘transaction do’ block but how do I do it in t...

Ruby on Rails: Model.all.each vs find_by_sql("SELECT * FROM model").each ?

I'm fairly new to RoR. In my controller, I'm iterating over every tuple in the database. For every table, for every column I used to call SomeOtherModel.find_by_sql("SELECT column FROM model").each {|x| #etc } which worked fine enough. When I later changed this to Model.all(:select => "column").each {|x| #etc } the loop starts out a...

Rails controller when no ActiveRecord model ?

Hello, I'm trying to create a rails web app that does not use ActiveRecord framework as I have no relational database back end. So far I managed to create a simple example application that works perfectly for listing, showing and editing my records. However I'm facing some problems when it comes to the record creation. The issue is...

SQLServer Binary Data with ActiveRecord and JDBC

I'm using the activerecord-jdbc-adapter with ActiveRecord to be able to access a SQLServer database for Rails Application running under jRuby and am having trouble inserting binary data. The Exception I am getting is below. Note I just have a blurb for the binary data from the fixtures that was working fine for MySQL. ActiveRecord::Stat...

Where's the best practice location for non-table-related classes in Rails apps?

Where do most Rails apps usually keep their non-active-record classes? In app/models? In lib/some_file.rb? Someplace else? ...

Creating has_many relationships in ActiveRecord without :primary_key

I'm working with an older version of Rails and ActiveRecord that does not have the :primary_key option in has_many relationships of more recent ActiveRecord versions and unfortunately I don't get to control when we upgrade. Is there any way to hack this solution via :conditions or :finder_sql options? ...

Rails AR validates_uniqueness_of against polymorphic relationship

Is it posible to validate the uniqueness of a child model's attribute scoped against a polymorphic relationship? For example I have a model called field that belongs to fieldable: class Field < ActiveRecord::Base belongs_to :fieldable, :polymorphic => :true validates_uniqueness_of :name, :scope => :fieldable_id end I have several...

rails validate_format_of non-negative integers

Hi, I am trying to validate the format of non-negative integers with the following validates_format_of :fundays, :with => /\A[\d]+\Z/, :message => "invalid fundays" And here is the form field used in the view <%= f.text_field :fundays, :maxlength => 3, :style => 'width:50px;' %> However, when I input a non-digit into this field an...

Rails validation "failing when succeeding"

I have this in my user.rb: attr_accessor :old_password def validate unless password.nil? errors.add_to_base("Old password entered incorrect") unless self.valid_password? old_password end end I have old_password as a a virtual attribute that has to be validated as matching with the current before updating to a ne...

Subsonic 3.0 Active Record Mysql connection issues with remote server

Hello, Updated below I couldn't describe the question any better without going into detail. I am having major issues with development/deployment of projects using SubSonic. Up until now everything has been fine but this is seriously hampering my progress. The project, while not important, is a CMS/Shopping Cart. I am using Visu...

Monitor database table for external changes from within Rails application

I'm integrating some non-rails-model tables in my Rails application. Everything works out very nicely, the way I set up the model is: class Change < ActiveRecord::Base establish_connection(ActiveRecord::Base.configurations["otherdb_#{RAILS_ENV}"]) set_table_name "change" end This way I can use the Change model for all existing re...

rails arguments to after_save observer

Hi, I want users to enter a comma-delimited list of logins on the form, to be notified by email when a new comment/post is created. I don't want to store this list in the database so I would use a form_tag_helper 'text_area_tag' instead of a form helper text_field. I have an 'after_save' observer which should send an email when the comm...

How to convert records including 'include' associations to JSON.

If I do something like: result = Appointment.find( :all, :include => :staff ) logger.debug { result.inspect } then it only prints out the Appointment data, and not the associated staff data. If I do result[0].staff.inpsect then I get the staff data of course. The problem is I want to return this to AJAX as JSON, including the staff ...

Rails find by *all* associated tags.id in

Hi Say I have a model Taggable has_many tags, how may I find all taggables by their associated tag's taggable_id field? Taggable.find(:all, :joins => :tags, :conditions => {:tags => {:taggable_id => [1,2,3]}}) results in this: SELECT `taggables`.* FROM `taggables` INNER JOIN `tags` ON tags.taggable_id = taggables.id WHERE (`tag`.`ta...

Why does this Rails named scope return empty (uninitialized?) objects?

In a Rails app, I have a model, Machine, that contains the following named scope: named_scope :needs_updates, lambda { { :select => self.column_names.collect{|c| "\"machines\".\"#{c}\""}.join(','), :group => self.column_names.collect{|c| "\"machines\".\"#{c}\""}.join(','), :joins => 'LEFT JOIN "machine_updates" ON "machine_upd...

default_scope in rails 3

I know named_scope has been changed to scope in rails 3. How do I perform default_scope in rails 3, I've had a good google but found nothing for defaults scopes. ...

@user.posts.where('status = ?', :unfinished).all returns []

By @user.posts, I can see there is a post with :unfinished status. But @user.posts.where('status = ?', :unfinished).all returns an empty array. I've tried to invoke @user.reload first, but it doesn't resolve the problem. (rdb:568) @user.posts [#<Post id: 1, content: "hehe", user_id: 1, created_at: "2010-04-03 06:16:47", updated_at: "2...

How to build a JSON response made up of multiple models in Rails

First, the desired result I have User and Item models. I'd like to build a JSON response that looks like this: { "user": {"username":"Bob!","foo":"whatever","bar":"hello!"}, "items": [ {"id":1, "name":"one", "zim":"planet", "gir":"earth"}, {"id":2, "name":"two", "zim":"planet", "gir":"mars"} ] } However, my User an...