activerecord

ActiveRecord Attributes Quesion

I have the following code in an Active Record file. class Profile < ActiveRecord::Base attr_accessor :tmp_postal_code def postal_code=(postal_code) @temp_postal_code = postal_code[:first] + "-" + postal_code[:second] end def postal_code @temp_postal_code end end I first overwrote postal_code=(postal_code) because p...

Optimize Rails eager loading query for find all

In a Rails 2.3.5 application I've got something like the following models: class Foo < ActiveRecord::Base has_many :bars end class Bar < ActiveRecord::Base belongs_to :foo end And when I'm calling Foo.all(:include => :bars) I see the following queries in console: SELECT * FROM "foos" SELECT "bars".* FROM "bars" WHERE ("bars...

find_by_field not working properly in ActiveRecord

I have a navigation system that provides a permalink for any link setup. When i run the line through script/console this is what it returns: >> Navigation.find_by_permalink("gems") => #<Navigation id: 10, text: "Gems", parent: nil, destination_controller: "pages", destination_action: "show", destination_id: "1", permalink: "gems", crea...

"Table not mapped" with NHibernate + Active Record

I'm using Active Record with ActiveRecord Facility, and am trying to use a custom NHibernate query. Do I need to define a mapping for a class even though it extends ActiveRecordBase and has ActiveRecord attribute? [ActiveRecord("VotesOnQuestions")] public class VoteOnQuestion : ActiveRecordBase<VoteOnQuestion> { [CompositeKey] p...

NHibernate & Castle ActiveRecord - learn by doing or RTFM?

I'm a complete noob to NHibernate & ActiveRecord. What would you say is the best way to start using them productively - following the manuals step by step, or progress in small steps by actually using it and cross the problems as I encounter them? ...

Do we use Rails ActiveRecord as a Hybrid Structure, i.e. Data Structure+Object ?

I have been using Rails for over 4 years so obviously I like Rails and like doing things the Rails Way and sometimes I unknowingly fall to the dark side. I recently picked up Clean Code by Uncle Bob. I am on Chapter 6 and a bit confused whether we as rails developers break the very fundamental rule of OO design, i.e. Law of Demeter or e...

Ruby on Rails ActiveRecord find_by-sql field names

Hi all, I'm doing custom find_by_sql queries which are dynamically created by user input. What is the best way to find the field names returned by find_by_sql I've tried using columns, column_names and keys methods but none work. Is the ActiveRecord result a hash or an array? e.g. @fm_reports = FmReport.find_by_sql(crosstab_query) fiel...

Subsonic 3 ActiveRecord Setup() behavior

Been a long time user of Subsonic 2.x and have used 3.x a bit, but I've recently started transitioning our use of SS from using repositories to ActiveRecord instead. I'm currently stumbling on some of our unit tests and I wonder if it's perhaps because I'm misunderstanding the intent of the Setup() method. Alas, the only documentation ...

Rails timezone ignored for ActiveRecord find?

I'm implementing the Timezone support in Rails 2.1+, and I've run into an apparent bug in the way the data is pulled from the db. Let me try to set it up. The "deals" model contains an "offer_date" datetime field. Let's say I have two records with these offer_dates: Deal 1: 2009-12-29 23:59:59 -0500 Deal 2: 2009-12-28 23:59:59 -0500 ...

Rails join three models and compute the sum of a column

Hi, I need some help over here to understand how the model relationship works on rails. Let me explain the scenario. I have created 3 models. Properties Units Rents Here is the how relationship mapped for them. Model #property.rb class Property < ActiveRecord::Base has_many :units has_many :rents, :through=> :unit ...

Big SQL SELECT performance difference when using <= against using < on a DATETIME column

Given the following table: desc exchange_rates; +------------------+----------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | time | datetime | NO | MUL | NULL | | | base_currency | varchar(3) | NO | MUL | NUL...

ActiveRecord has_one relationship does not return in certain cases

Given three models that are each nested in each other. If I create the top-level object and build_* the other child objects, I can retrieve all child objects through the relationships before and after save() on the original instance. However, if I try to retrieve the 2nd level nested object after find(:id) the original parent it fails. I...

Hacking attr_accessible to support a different set of accessible attributes for create and update

I'm trying to figure out how to make this hack for attr_accessible to support a really common use case in my code, but really after looking at attr_acessible and ActiveRecord::Base source code for a while, still don't know where to start. I can probably figure it out after digging deeper, but first I'd like to ask if anyone else would f...

How to process a large ActiveRecord result set in groups

I'm wondering if there is a way to take an array of ActiveRecord results (or any array, for that matter) and process it in groups of 25 or so. Something like this: User.all.each(25) do |group| # Some code that works with this group of 25 end I'm just looking to avoid doing multiple successive database queries. Thanks! ...

Multipe relationships in Rails where the class name does not match the association name

I have a private message model that relates to two users, how do I setup an association so that PM.sender is the sender's user model and PM.receiver is recipient's user model? (so that I can call PM.sender.username etc.) I have a sender_id and receiver_id field. ...

How can I validate the data calculated on the basis of associated models

Hello, I have a posts model which has_one reposts and has_one sponsored. User can purchase reposts and sponsored posts while creating a post. I want that the minimum purchase(sum of reposts and sponsored purchases) be atleast 1$. so i want to validate this in the post model but i can't figure out on how to write such ...

Are there any issues with Activerecord class in codeigniter?

I'm a little hesistant against to use it because I believe there can be some issues with it, but I don't really know it before I've tried it. Or is it good enough to use it or should I do plain sql statements? Does anyone know? ...

Rails: Why does the model method not work?

Hi, its the first time I post here. I have a problem that i can somehow not solve. Just for the record, I know what instance and class methods are (even if I may not understand them completely ;-) Here is my model code: class Car < ActiveRecord::Base has_many :drives has_many :users, :through => :drives def self.user_ids() ids = [] ...

code igniter, custom selecting data using active record

Hi fellow programmer I want to ask if I need to do this query in my app select qty, type from tItem where qty=0 and (type=1 or price=100) How do I do that using active record in code igniter? because if i do $this->db->where('qty','0'); $this->db->where('type','1'); $this->db->or_where('price','100'); the query would be like se...

Can I tell activerecord to not convert "datetime" data to objects when I access?

I have some code that simply accesses a datetime field so activerecord converts it to a Time object automatically when I read it: @some_appointment.some_time The problem is that sometimes the "some_time" datetime column has bad data. "0209-12-20" instead of "2009-12-20". This causes to Ruby to throw a "year too big to marshal" error...