activerecord

ActiveRecord select attributes without AR objects

I have a table with MANY rows, I need just the IDs of certain rows. The slow way is to call SomeARClass.find(:all, :conditions => {:foo => true}, :select => :id) This returns AR Objects... Is there a way to call a select on a class and have it return a plain old ruby data structure. Something like this: SomeARClass.select(:id, :con...

Rails ActiveRecord: pretty errors when deleting dependent entities with foreign keys constraints

I have in Rails application several tables with foreign keys constraints. For example, every order belongs to a customer. There's a costumer_id column on the orders table. When I delete a costumer with a placed order, because of database constraints, MySQL returns the error: Mysql::Error: Cannot delete or update a parent row: a fo...

observe activerecord relations

hi Stack, I like to observe adding an object to my has_many relation without saving them to the database. So when I add a LineItem to my Order I like to call Order::calculate_total to update the actual total value. o = Order.new o.line_items << LineItem.new # should call calculate_total from order-object but there are no observers for ...

What attributes are updated in model

Hi all, Is there a way I can know what attributes are updated in my model? I only want to do a validation on a user when a certain attribute was posted. This would actually be the hash that is sent via the params eg: @user.update_attributes(params[:user]) Thanks. ...

Rails - Create/Destroy record in join table

I'm working on this record insert/delete I'm not sure what the syntax is to perform the query. I have a user model and an event model. I have created a joining table called Personal that stores the user_id, and event_id of any events that the users like. I created an "Add" method in my events controller so whenever someone clicks it r...

Polymorphic class creating in Castle Project ActiveRecord

I am using ActiveRecord in order to store my classes in the database. I have classes hierarchy, say it Man and Woman inherited from class Human. Human database table contains field Sex which is 0 for man and 1 for woman. I'd like to be able to load class Human with regards to Sex field, that is, if Sex is 0, loading of class Human shoul...

Retrieve all association's attributes of an AR model?

Hello, How do you think is the more optimum way to retrieve all the attributes for each association that an AR model has? i.e: let's say we have the model Target. class Target < ActiveRecord::Base has_many :countries has_many :cities has_many :towns has_many :colleges has_many :tags accepts_nested_attributes_for :countries...

Modeling relationships between spots and checkins ala Foursquare

Here's the scoop: As a learning exercise, I'm trying to write a Rails clone of one of the many location-based games out there (Foursquare, Gowalla, etc.) I have Users who create and check in to Stores. In ActiveRecord terms: :user has_many :stores :store belongs_to :user But now I've created a third model - Checkins. The table behin...

How/where to temporarily store ActiveRecord objects if not in session?

I'm refactoring a Rails-based event registration application that has a checkout process that hits several ActiveRecord models. Ideally, the objects should not be saved unless checkout is complete (payment is successfully processed). I'm not entirely certain why it would be a bad thing to serialize these objects into the session tempor...

check if the value of a field changed in a before_update filter

Hi I have a database field where I want to store my password. In a before_create filter in my model I call a encryption function and save from clear text to encrypted text. I want now to use the before_update also for encryption, but only if the value has changed. How can I write a condition for checking if a field value has changed? ...

Undefined method error on .sum

Hi Guys, I have a error that I can't find the solution. When I run: Week.find(1).results.sum('box') A get a SUM of column box an it works perfect! But when I apply a filter in it: Week.find(1).results.find(:all, :joins => [:seller],:conditions => ['sellers.user_id = ?', 1]).sum('caixas') I get a error NoMethodError: undefined me...

add method to reflection-object and named-scopes

I Like to add a method to my has_many relation in the way that it is applyed on the relation object. I got an Order wich :has_many line_items I like to write things like order.line_items.calculate_total # returns the sum of line_items this I could do with: :has_many line_items do def calculate_total ... end end but this w...

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console, I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attr...

Rails ActiveRecord: Is a combined :include and :conditions query possible?

Imagine I have wiki Articles, with many Revisions. I'd like to do a query with ActiveRecord through the database, which only returns those Articles which have Revisions which are updated in the past 24 hours. Is such a thing possible? I'd imagine it'd be something like: Articles.find_all(:include => :revisions, :co...

Rails ActiveRecord updating fields

My Example: class Category < ActiveRecord::Base has_many :tags, :as => :tagable, :dependent => :destroy def tag_string str = '' tags.each_with_index do |t, i| str+= i > 0 ? ', ' : '' str+= t.tag end str end def tag_string=(str) tags.delete_all Tag.parse_string(str).each { |t| tags.build(:tag...

How to avoid updating update_at flag while updating associated model in active record?

My association to models as follows People model belongs_to :category has_one :account, :through => :category category model belongs_to :account has_many :bookings account model has_many :categories Level model accepts_nested_attributes :peoples I wrote @level.update_attributes(params[:level]) in Le...

rails build method for complex model with days of the week

I have a set of nested models for storing prices for individual rooms. Ie. Places Rooms Room_rates Each model has the necessary accepts_nested_attributes_for and has_many belongs_to association and I have a form and a build method which works perfectly for the initial creation. My question is how to make a smarter contro...

Can I :select multiple fields (*, foo) without the extra ones being added to my instances (Instance.foo=>bar)

I'm trying to write a named scope that will order my 'Products' class based on the average 'Review' value. The basic model looks like this Product < ActiveRecord::Base has_many :reviews Review < ActiveRecord::Base belongs_to :product # integer value I've defined the following named scope on Product: named_scope :best_reviews, ...

Does ActiveRecord allow for custom types?

I have a scenario where I am trying to do a proof of concept using Rails on a legacy db2 database to get buy off that it is a viable option platform for us to use for current and future development. All of our tables have a primary key which is defined as char for bit data, which is binary. Is there any way I can define a mapper to c...

ActiveRecord pulls records out in ASCII encoding in Ruby 1.9

Hello, I'm trying to migrate my app to Ruby 1.9, however ActiveRecord keeps retrieving records out of my MySQL database with an ASCII encoding, causing "incompatibility between utf-8 and ASCII" like errors. I've tried setting the "encoding: utf-8" in the database.yml file, and I've also tried putting " #coding: utf-8 " at the top the err...