activerecord

Ruby 1.8.7 compatibility

I had a exception when I switch to Ruby 1.8.7 on Snow Leopard ArgumentError: wrong number of arguments (1 for 0) /Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/connection_adapters/abstract/quoting.rb:27:in 'to_s' /Library/Ruby/Gems/1.8/gems/activerecord-1.15.5/lib/active_record/connection_adapters/abstract/quo...

Rails Associations - Callback Sequence/Magic

Taking following association declaration as an example: class Post has_many :comments end Just by declaring the has_many :comments, ActiveRecord adds several methods of which I am particularly interested in comments which returns array of comments. I browsed through the code and following seems to be the callback sequence: def has_m...

does rails postgres adapter support ssl?

i'm trying to configure a rails app to remotely connect to a postgres db. i've noticed that the connection adapters for mysql have options that specify the required info for setting up an ssl connection, but there is no equivalent options for the postgres/pg adapter. after googling around, i haven't been able to find anything either (on...

Different DB connection for individual model depending on environment

Hi there, I have a Model (MessageImporter) that connects to a different Database than the other models using self.establish_connection. Everything works fine when I pass the connection info hardcoded. Now I need the connection to depend on the current environment. So I added the info to my application_config.yml (it's a simple nifty_con...

Dump queries made by ActiveRecord to file?

Is there an easy way to dump / write the queries performed by ActiveRecord into a file? I know that they are in the log, but without parsing that? How can I get the queries? ...

Rendering a Rails view to string for email

I'd like to send emails from a table every night at midnight, so I've created a model to track all communications to be sent. This Communication model stores who the letter is to, from, and the contents of the letter to be sent as html. The user enters a criteria to match for whom the letter is intended: state, country, favorite color,...

rails method to get the association name of a model

Is there a way to find out what associations a model has? Take these 2 models: class Comment < ActiveRecord::Base belongs_to :commentable end class Post < ActiveRecord::Base has_many :comments belongs_to :user end I'm looking for something like: Post.has_many #=> ['comments', 'tags', ...], Post.belongs_to # => ['user'] and Comm...

I have a bunch of listings, I am trying to sort by featured and date in rails yet it's not working properly...

Let's say I have a listing a listing has a date and a featured (of true or false) How do I order my listings to order by featured = true first? Currently I have some listings as featured nil, then a few appeared as false - the ones that appeared as false are showing up before featured true. This makes sense because false is before tru...

ActiveRecord - automatically merging model data as an aggregate

Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :type, :default => 'User' t.string :user_name, :null => false t.boolean :is_registered, :default => true # ... many more fields end end end class CreateContactInfo < Active...

Convert data when putting them into a database using active record

What's the cleanest way to convert data (field values) when storing/loading into/from a database. I usually convert the values the user has entered in the model class: def date_str format_date(self.date) end def date_str=(str) self.date = DateParser.parse(str) end (Note: The internal date parser is not sufficient, i...

rails active record - complicated conditions clause

this works: ids = [1,2] varietals = Varietal.find(:all, :conditions => [ "id IN (?)",ids]) But what I want to do is that plus have a condition of: deleted => false varietals = Varietal.find(:all, :conditions =>{ :deleted => false}) any ideas? am i going to have to use find_by_sql? ...

prepared statements in rails/activerecord

does rails activerecord have any support for prepared statements? best I can tell -no. ...

How do I cache ActiveRecord model objects across redirects?

This is possibly a newbie question, but I'm not sure what terms to search for. Say I have a CUSTOMER object, and I want to send a MESSAGE to that customer. What I would do first is add a SENDMESSAGE action on the CUSTOMER controller, which builds the message object. (Assume this is the right thing to do?) In this instance however, ra...

How do I create different sessions for different windows in a desktop application with ActiveRecord?

I'm building a desktop application with Castle ActiveRecord and want to be able to do the equivalent of 1 nHibernate session per window form. Any ideas on how can I do this with Active Record? Specifically, I have a main window that allows you to browse the data (read-only) and then you can open separate forms to edit the data. Each...

options inheritance

Hi there. I have a plugin module that extends the AR with a before_save callback to log all changes made into a relating acts_as_commentable comment. This works fine. However, I want to add more details to the comment such as who made the change etc. I have made a couple of fields available to the model instance log_message and log_ow...

Rails - build_association is not working for a has_one and belongs_to relationship.

I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client < ActiveRecord::Base has_one :subscription end but when I try to create a parent from the child e.g. sub.build_client the foreign key does not get set e.g. >> sub = Subscription.new => #<Subscription id: nil, token: nil, user_id: nil, c...

UNION with codeigniter's active record

Hi all How to do UNION query with codeigniter's active record? ...

Getting count of elements by `created_at` by day in a given month.

I wanted to make a simple chart of users that have been created in the past month in my app. Like basically for each day in the past month I want to show the count of users that have registered that day. What I have so far: # Controller @users = User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]) # View <% @us...

Rails - Paperclip validating attachment size when it shouldn't be?

I've got a rails model using Paperclip that looks like this: has_attached_file :image, :styles => { :normal => ['857x392#', :png] }, :url => '/assets/pages/:id/:basename.:extension', :path => ':rails_root/public/assets/pages/:id/:basename.:extension' validates_attachment_size :image, :less_th...

Representing ecommerce products and variations cleanly in the database

I have an ecommerce store that I am building. I am using Rails/ActiveRecord, but that really isn't necessary to answer this question (however, if you are familiar with those things, please feel free to answer in terms of Rails/AR). One of the store's requirements is that it needs to represent two types of products: Simple products - t...