activerecord

adding class methods to ActiveRecord::Base

I have created an instance method which is also a callback (if that makes sense) which does some stuff thats irrelevant. I would love to be able to just call: class Model < ActiveRecord::Base fix_camelcase_columns end Instead at the moment I have this: def after_find self.class.columns.each do |column| self.instance_eval("d...

Incorrect error

If you assign an invalid date (like December 39th) to a datetime column ActiveRecord returns a "can't be blank" error when is should probably return an error like "Not a valid date" My question. Is this expected rails behavior, a bug or, something that I could patch? class ExerciseLog < ActiveRecord::Base validates_presence_of :sche...

How to search in this activerecord example?

Two models: Invoice :invoice_num string :date datetime . . :disclaimer_num integer (foreign key) Disclaimer :disclaimer_num integer :version integer :body text For each disclaimer there are multiple versions and will be kept in database. This is how I write the ...

Setting many key/value pairs

Hi, I'm working on a rake task which imports from a JSON feed into an ActiveRecord called Person. Person has quite a few attributes and rather than write lines of code for setting each attribute I'm trying different methods. The closest I've got is shown below. This works nicely as far as outputing to screen but when I check the value...

get last insert id when using Activerecord

Hi, For Sqilte3 C API, I would use sqlite3_last_insert_rowid. How to get this id when using ActiveRecord after insert a new record? I use following way to insert a new record : Section.new |s| s.a = 1 s.b = 2 #I expected the return value of save to be the last_insert_id, but it is NOT s.save end ...

.save puts NULL in id field in Rails

Here's the model file: class ProfileTag < ActiveRecord::Base def self.create_or_update(options = {}) id = options.delete(:id) record = find_by_id(id) || new record.id = id record.attributes = options puts "record.profile_id is" puts record.profile_id record.save! record end end This gives me ...

In Rails, how to respect :scope when using validates_uniqueness_of in an embedded object form?

I have a Book model, which has_many Chapters (which belong_to a Book). I want to ensure uniqueness of Chapter titles, but only within the scope of a single book. The catch is that the form for creating chapters is embedded in the Book model's form (The Book model accepts_nested_attributes_for :chapters). Within the Chapter model: va...

Rails, ActiveRecord, how to find foreign rows that are NOT there?

The title is a bit difficult to put together for this question. Say I have two models, Foo and Bar. Foo has many Bar's. Bar belongs to one Foo. How do I use Foo.find to find all of the Foos that currently have zero Bars? In SQL this would translate into something like: SELECT * from foo where id NOT IN (select foo_id from bar); ...

How can I make named_scope in Rails return one value instead of an array?

I want to write a named scope to get a record from its id. For example, I have a model called Event, and I want to simulate Event.find(id) with use of named_scope for future flexibility. I used this code in my model: named_scope :from_id, lambda { |id| {:conditions => ['id= ?', id] } } and I call it from my controller like Event.fr...

Any exprience with Castle ActiveRecord?

Hi all, I was searching for a light data access framework based on NHibernate. I needed simple CRUD and some simple HQL or LINQ-to-NHhibernate queries. Performance was not an important issue and applications which I'm working on have simple table structure but many tables. This data access framework is going to be used in a ASP.NET Webf...

Preventing ActiveRecord save() on an instance

I have an ActiveRecord model object Foo; it represents a standard database row. I want to be able to display modified versions of instances of this object. I'd like to reuse the class itself, as it already has all the hooks & aspects I'll need. (For example: I already have a view that displays the appropriate attributes). Basically I wa...

Modifying association arrays on cloned ActiveRecord objects

I have an ActiveRecord model class Foo that has_many Bar. I want to clone a Foo (to get duplicates of most of its attributes) and then modify its Bar instances. This is a problem because cloned ActiveRecord instances share the same associated array; changes to one affect the other. f1 = Foo.new b = Bar.new f1.bars << b f2 = f1.clone f2...

Rails 2.3: How to turn this SQL statement into a named_scope

Having a bit of difficulty figuring out how to create a named_scope from this SQL query: select * from foo where id NOT IN (select foo_id from bar) AND foo.category = ? ORDER BY RAND() LIMIT 1; Category should be variable to change. What's the most efficient way the named_scope can be written for the problem above? ...

trying to use ActiveRecord with Sinatra, Migration fails question

Hi, running Sinatra 1.0, I wanted to add a database table to my program. In my Rakefile I have a task task :environment do ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["development"]) end I have a migration task in my namespace that calls the migration code: namespace :related_...

ActiveRecordStore InvalidAuthenticityToken

I have recently been using cookie store and I want to transition to active record store. However I keep getting an invalid authenticity token. After deleting my cookies, I was able to access the page just fine, but I don't want all my users to come to my page, get a huge error and then figure out that I want them to delete their cookies....

rake test not copying development postgres db with sequences

I am trying to develop a rails application on postgresql using a sequence to increment a field instead of a default ruby approach based on validates_uniqueness_of. This has proved challenging for a number of reasons: 1. This is a migration of an existing table, not a new table or column 2. Using parameter :default => "nextval('seq')" di...

Commenting out protect_from_forgery

Hi, I was trying to use active record store but I kept getting an invalid authenticity token. Someone told me to remove my protect_from_forgery from application controller. I know that this would remove all auth tokens but I'm not sure if this is a good idea. Does active record store not need auth tokens? By the way, all I need is a wa...

Combining Two Models in Rails for a Form

I'm very new with rails and I've been building a CMS application backend. All is going well, but I would like to know if this is possible? Basically I have two models: @page { id, name, number } @extended_page { id, page_id, description, image } The idea is that there are bunch of pages but NOT ALL pages have extended_content. In t...

[Rails3] How to do multiple many to many relationships between the same two tables.

Hi. I have a model of a club where I want to model the two entities Meeting and Member. There are actually two many-to-many relationships between these entities though, as for any meeting a Member can either be a Speaker or a Guest. Now I am an OO thinker, so would normally just create the two classes and each one would just have two a...

How to get array with clear data from Active Record query without using map

Hi I would like to ask if its possible get array with clear data from ActiveRecord query without using map, collect or each. names = User.find(:all, :select => "name") return names == [#<User name:"Peter">,#<User name:"Martin">] and I want names == ["Peter", "Martin"] without using map, collect or each. Thanks for your answers. ...