activerecord

Save Rails ActiveRecord objects into a temporary table (MySQL)

A user can import data into our website from a file. The data normally contains several hundred Items (Item < ActiveRecord::Base). Although the validations help, they cannot solve the problem of sanity-checking the content. For that we would like to have a test mode. Could we use a temporary Items table for this with Rails/MySQL, and,...

Is there a shorter name for Rails find_or_initialize_by_this_that_and_theotherthing?

I use the active record find_or_intialize (similar to find_or_create) in a few places, which leads to annoyingly long non-breakable method names. For example, "find_or_initialize_by_topic_id_and_publish_date_and_publist_id" really messes up my formatting. I know with the normal find method I can supply all this stuff as parameters. Is th...

How do you scope ActiveRecord associations in Rails 3?

I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many"). I have records which have permission columns. I would like to build a default_scope that takes my permission columns into consideratio...

Ruby on Rails: Best way to tie together two models that correspond one to one

If I have two models that are guaranteed to have a one-to-one correspondence, i.e. if one is created, I will always also need the other, and if one is deleted, I will also want to get rid of the other, what's the best way to tie them together? I see that the has_one/belongs_to :dependent method takes care of the deletions, but I don't s...

What's the significant difference between active record and data mapper based ORMs?

Like doctrine(active record) and Xyster(data mapper),what's the difference? ...

Rails autosave association in controller action

I have the following one to many associations. Document has many Sections and Section has many Items. class Document < ActiveRecord::Base has_many :document_sections, :dependent => :destroy, :autosave => true has_many :document_items, :through => :document_sections end class DocumentSection < ActiveRecord::Base belongs_to :docume...

Is this possible to do in one query?

I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to :recipient, :class_name=> 'User' end I want to fetch all the buddies of a given user ordered by most recent date of message what appears in conversation between given user and his buddy and, if it possible in same query, to fetch the...

Changing created_at for seed.rb data

I'm using seeds.rb to load some dummy data into my project as I develop it. I'd like to use a random created_at date for my records, but the created_at date is always set to Time.now on create. #seeds.rb Project.create :name => 'Dummy Project', :created_at => Date.today - rand(10).days ...

Make all subclasses of ActiveRecord::Base methods say their name.

For cruft-removal purposes I would like to log whenever a method from one of my AR models is called. I can get get all those classes with something like this: subclasses = [] ; ObjectSpace.each_object(Module) {|m| subclasses << m if m.ancestors.include? ActiveRecord::Base } ; subclasses.map(&:name) But then I need a list of only the ...

how do I get foreign_key to work in this simple has_many, belongs_to relationship?

I'm pulling data from Harvest. Here are my two models and schema: # schema create_table "clients", :force => true do |t| t.string "name" t.integer "harvest_id" end create_table "projects", :force => true do |t| t.string "name" t.integer "client_id" t.integer "harvest_id" end # Client.rb has_many :projects, :for...

Is there any way to have a one_to_many relationship(where many > 0) in Rails?

I know Rails has a one_to_many relationship, but I want to enforce that "many" is at least one object. What's the most elegant way to do this? ...

Ruby, text parsed from XML feed with Nokogiri having encoding issues on display through PHP

I'm grabbing an XML feed that claims to be <?xml version="1.0" encoding="ISO-8859-1"?> with Nokogiri and inserting the text into mysql with activerecord (OUTSIDE of rails). Using HTMLEntities to decode. Here is an example (&#147;LON: SDM&#148;) I can't seem to handle some of these html special characters properly. When redisplaying...

rails/mysql - how do i silence out ActiveRecord::StatementInvalid error?

i am writing a code to handle read/unread messages, with a simple user_id/message_id mysql table to handle read/unread status. when the user views the message, i execute Reading.create(:user_id => uid, :message_id => mid) there is unique index on user_id/message_id fields combination, so when the entry in Readings already exists, i g...

Changing the default ActiveRecord id from 0 to 1000000

Hello there, I'd like to change the minimum for the id of created objects from 1 to 1000. So when I create in rails my first model object it gets the ID 1000 and not 1. Is there a way to set this in the schema/migration files? ...

Strange error coming from ActiveRecord (probably)

My development environment: Ubuntu 9 Ruby 1.9.1/1.8.7 (rvm) Rails 2.3.5 Mysql 5.0 Apache Passenger Below is the part of the program flow to represent the issue. Request comes: #action def create begin @report = Report.new(params[:report]) ... rescue LocationNotFound => e ... end end Report constructor: class ...

Fetch all entries exept those with empty column in Rails

How do I fetch all 'link' entries exept those with an 'url' column that is blank? My controller looks like this: def show @user = User.find_by_username(params[:username]) @links = @user.links.all respond_to do |format| format.html # show.html.erb format.xml { render :xml => @links } end end Thanks in...

Atomically increment a field using SubSonic 3 ActiveRecord.

I'm tring to increment a field in a MySQL database using SubSonic 3 ActiveRecord. In SQL, this is what I'm after: UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to; I tried the following, but it didn't seem to work (messages_received always seemed to be 1): _db.Update<person>() .Set("messages_receive...

Changing ActiveRecord attribute value in before_save hook

I needed to fix the encoding of an ActiveRecord attribute and decided to do it in a before_save hook. And at this point I noticed an unexpected feature. When I wanted to change the value of the attribute, simple using the attribute_name=XY did not work as I expected. Instead of that I needed to use self[:attribute_name]=XY. So far did no...

Group by with ActiveRecord in Rails

Hello, I have a the following table with rows: =============================================== id| name | group1 | group2 | group3 | group4 | =============================================== 1 | Bob | 1 | 0 | 0 | 1 | =============================================== 2 | Eric | 0 | 1 | 0 | 1 | ==...

group by country with ActiveRecords in Rails

Hello, I have a table with users: name | country | .. | UK | .. | US | .. | US | .. | UK | .. | FR | .. | FR | .. | UK | .. | UK | .. | DE | .. | DE | .. | UK | .. | CA | . . What is the most efficient way with ActiveRecord to get the li...