activerecord

Active Record to_json\as_json on Array of Models

First off, I am not using Rails. I am using Sinatra for this project with Active Record. I want to be able to override either to_json or as_json on my Model class and have it define some 'default' options. For example I have the following: class Vendor < ActiveRecord::Base def to_json(options = {}) if options.empty? super ...

Right way to force uniqueness on a join model? (has_many :through)

I have a parent/child relationship via our users table, with models as such: class User < ActiveRecord::Base # Parents relationship has_many :children_parents, :class_name => "ParentsChild", :foreign_key => "child_id", :dependent => :destroy has_many :parents, :through => :children_parents # Children relatiopnship has_many :...

Rails: Updating two fields of an ActiveRecord instance simultaniously

I have a model, called Book, that has the fields title and filename (and other fields but they are unrelated). I'd like that the filename field was automatically created using the title, thus in my Book.rb: class Book < ActiveRecord::Base # ... def title=(title) self.filename = sanitize_filename(title) self.title = title ...

Finding a node with no children in a tree like structure with Rails

Hi, I have a tree like structure roughly like this: class Node < ActiveRecord::Base belongs_to :parent, :class_name => self.to_s, :foreign_key => 'parent_id' has_many :children, :class_name => self.to_s, :foreign_key => 'parent_id', :dependent => :destroy ... end I can load all nodes that don't have a parent with this sco...

Modifying a model attribute won't make it to the DB in Yii's CActiveRecord::beforeSave()

I'm implementing HTML sanitization for form fields, on the beforeSave() method of a model. But it doesn't work. Example: public function beforeSave() { $this->anAttribute = 'somevalue'; var_dump( $this->anAttribute ); } somevalue is actually outputted, but it never gets to the DB, so in between beforeSave() and save() the value is l...

Active Record Rails 3 associations not working properly

I have the following classes with associations: class Customer < ActiveRecord::Base has_many :orders has_many :tickets, :through => :orders has_many :technicians, :through => :ticket has_many :services, :through => :ticket end class Order < ActiveRecord::Base belongs_to :customer has_many :tickets has_many :technicians,...

Retrieve all posts where the given user has commented, Ruby on Rails

I have users, posts and comments. User can post only one comment to each post. class User < ActiveRecord::Base has_many :posts has_many :comments end class Post < ActiveRecord::Base has_many :comments belongs_to :user end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end On userpage (http://host/us...

Rails Active Model - scope only objects which have existing members in has_many relation

This seemed trivial at first, but I can't get it right for some time now. The relation is trivial. class Project < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :project end Now I would simply want to get all the Projects which have 1 or more tasks associated. Now to do this without any extende...

Why does associated object not save?

I have a ruby (on rails) class: class User < ActiveRecord::Base # relationships belongs_to :current_shipping_address, :class_name => "Address" belongs_to :subscription # Validators validates_presence_of :subscription validates_presence_of :current_shipping_address end I do this in a controller: subscription = Subscriptio...

Soliciting Rails Association Feedback + Questions from a Noob

hi All, Preface: If you hang out in #rubyonrails on freenode this may sound like an echo to you as i asked it in there 2 days ago. After spending a number of hours researching AR associations, following my discussions in #rubyonrails, i still feel lost so I'm asking here. :) Goal I host a number of blogs. My intent is to create ba...

before_save , before_validation :on => :save

I wanted add a hook on before_save. Validation happens before "before_save" is called, so I moved it to before_validation :on =>:save. Now the problem is, when I do @object.save_without_validation (sometimes I had to use this). my before_validation hook doesn't get called. Am I missing something or is there a more robust way of adding...

ActiveRecord relation using an array of foreign keys

Hi, Is that possible to establish a relationship between Tree and Branch such as: class Tree < ActiveRecord::Base has_many :branches end class Branch < ActiveRecord::Base belongs_to :tree end But with an array of foreign keys branch_ids stored in Tree? I know it's the opposite of the default process, but I want to do so (just fo...

Is there a more eloquent way of creating a table that has a variable number of columns: Active Record & Rails 3?

Currently I have an Order class. each order has 1 to infinite number of items. the number of items is not known until run time. since there is no field type of Array in active record / rails, how do you create a variable number of columns? The only way I can think of is to specify a bunch of ticket columns ahead of time; but is very...

Override a scope in rails 3

I want to override an existing scope to add an extra condition to it. I've shown my attempt to do this using alias_method. Unfortunately this approach doesn't work with scopes, I get an undefined method error. How do I do it with scopes? module Delayed module Backend module ActiveRecord class Job < ::ActiveRecord::Base ...

Merge of 2 active record arrays very slow?

I have 2 sets of data as following: default_products - A set of active record objects retrieved from memcached owned_products - A set of active record objects retrieved from the database Retrieving the data is fast and i get around 30-40 req/s when just returning either one of the arrays yet as soon as i do the following to return both...

ActiveModel based class does not create the same results as an ActiveRecord equivilent

Hi, I am developing a Rails 3 app in a largely tabless capacity. I am using savon_model and ActiveModel to generate similar behaviour to ActiveRecord equivalents. Below is my code: class TestClass include Savon::Model include ActiveModel::Validations # Configuration endpoint "http://localhost:8080/app/TestService" namespac...

Is the activerecord pattern built into Entity Framework 4.0?

In the past I used Sub Sonic which has the activerecord pattern baked into the framework. With Sub Sonic it was very easy to find the "dirty" fields on an update. I now have a need to create an audit table in my application that utilizes Entity Framework 4. Is there a comparable feature in EF 4 that will give me the dirty fields? Thank...

ActiveRecord Association without Foreign Key

I am trying to build a relationship model between users. A user can either initiate a relation, or receive a relation from another user. Therefore, the relations table in the db has the foreign keys "initiator_id" and "recipient_id". Now, I can figure what relations the user initiated or received using the following associations: has_...

Save active_record without calling callbacks in rails3

In rails 2 there was a private method on active_records called create_without_callbacks which you could call to save a record into the database without triggering the callbacks associated with that object. This method has disappeared in rails 3, is there any way to achieve the same thing? ...

Alias for column names in Rails.

Hello. I write web-GUI for application and must (!) using names for columns such as 'delete' or 'listen-control' and so on. I find this code but it is dated by 05 Aug 2005 and not works with Rails 3. How can I redefine this names using native Rails mechanism? Is it possible? PS I'm know about problem with hyphens in the Rails. PPS ...