activerecord

There is no real 'prepared statement' in rails?

When we use ActiveRecord, we can use: User.find(:first, :conditions=>["name=?", name]) It looks like ActiveRecord are using 'prepared statement', but after looking into the code, I found ActiveRecord just use String.dup and connection.quote() to adjust the content to build a sql, not like Java. So, there is no real prepared statment ...

XML mapping and validation for Rails models

I am implementing an application that manipulate XML documents using Ruby on Rails. I want to have models that encapsulate all the logic and then convert them to corresponding XML document when save. Although I do not need database persistence for my models, I want my models to have validation mechanism like ActiveRecord's. Also, for con...

Migrating procedural, antique CRUD code and proprietary DBMS to OO ORM on SQL

Please excuse my long-winded explanation, but I wanted to be as explicit as possible in the hopes of getting as much useful feedback on my situation as possible. You can skip to the questions at the bottom if you are impatient. Explanation At my current job, development is done in an antiquated language that is hard-wired to a proprie...

How to write a query in CodeIgniter Active Record

Hi all, I'm having some issues getting codeigniter active record to produce a query like this: SELECT fruits.* FROM fruits WHERE fruits.color = 'red' AND ( fruits.size = 'medium' OR fruits.name = 'kiwi' OR fruits.length = 7 ); Basically I want to have several or clauses but one where clause that is always enforced. $this->db->select(...

problems with bulk insertion and "bulk validation" in Rails

I'm using ar-extensions' import feature to do bulk import and it's quick, but not as quick as I'd like. Two problems I am seeing from the logs: I still see individual SQL insert statements - why isn't it doing multirow insertion? I have a :validates_uniqueness_of and I see that it does a SELECT for every row to do it. Is there a "bulk ...

Is it possible to determine whether an ActiveRecord attribute has been set, other than to the default?

We have an ActiveRecord model whose columns have some default values. It also has a validation condition such that, if the 'profile' property is set to a different value, then the default values are invalid. What I'd like is to be able to determine whether the attributes have been set since they were set to the default so that I can re...

Move schema_migration table to newer version without migrating?

Most of my migrations are by number, not timestamp, but 5 or 6 of them (the most recent) are with timestamp. These I want to move to number based, and now I want to forward the migration table to the appropriate number. How do I force the table to update without actually running the migration, or should I migrate down to 5 or 6 ago (befo...

In Rails/ActiveRecord 3, how do I change the default primary key type for MySQL?

In Rails 3, how do you change the default primary key type to, say, BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY In my case I'm only interested in MySQL. For Rails 2, you can see the answer to "How do I use long IDs in Rails?"1 In Rails 3, however, this will throw an error. I'm not sure if that's because the class is no long u...

Can't Join a model when using belongs_to

class Exercise < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :exercises end Why can't I find all Exercises there were created by users with the role of "admin"? Exercise.first :include => [:user], :conditions => ['user.role = "admin"'] The error shoes that the users table is not being join...

Ruby On Rails - many to many between the same table

I am trying to create a somewhat complex relationship in Rails, and am having some trouble finding the best way to do so. I have a Users table in which each user acts as a teacher and a student. I would like to have a has_many "students" (which are also just Users) and a has_many "teachers" (which are also just Users). I do not want to d...

How to insert primary key value explicitly?

I have a table called messages and here is the table structure, I don’t want id is auto increment field but it should be a primary key for that table. Here is table structure for messages CREATE TABLE `messages` ( `id` INT(11) NOT NULL, `user_id` INT(11) NOT NULL, `text` VARCHAR(255) NOT NULL, `source` VARCHAR(...

Storing arrays in database using ActiveRecord

I am on rails 2.3.8 & I am using mysql as db adapter. I want to store arrays in my database. After searching I could come up with this very useful article. Now I need to use GUI for input & not only server console. So say I have a text field called nums which logically should have int array. What should be the format of nums so that it...

foreign keys in activerecord migrations vs. schema.rb

I'm running activerecord 3.0.0beta. I know you can create columns with foreign keys like create_table "my_things" do |t| t.reference "other_thing_id" end but I forgot and just made it a plain integer. Now, I've added a migration like execute("alter table my_things add constraint fk_other_thing foreign key (other_thing_id) reference...

How to model a specific many to many association

Currently I have several models set up like so, class Account < ActiveRecord::Base has_many :office_hours has_many :staff_positions end class OfficeHour < ActiveRecord::Base belongs_to :account end class StaffPosition < ActiveRecord::Base belongs_to :account end class Document < ActiveRecord::Base end Now I need to set up a...

Rails efficient way to save non-duplicates?

I have a type I want to save to the database, say, email addresses, and I want to save them in the most efficient way possible. I also want to associate posts with email addresses, so if it exists, I want to assign the post to that email address. Should I do a search first and then go based on that result, or is there a more efficient wa...

Rails - User Session Control using Webservice instead ActiveRecord

Hello there, I'm trying to find some cool and nice way to manage the session control of my application, just like authologic, clearance. But the issue is that I don't have a database, all the users are stored in a external webservice and the Login function is basically call the LoginDB(user,password, salt) and then it returns true or fal...

rails - boolean operators in find

params[:codes] = "9,10" @result = Candidate.find :all, :joins => params[:codes].split(',').collect {|c| ["INNER JOIN candidates_codes on candidates_codes.candidate_id = candidates.id, INNER JOIN codes on codes.code_id = candidates_codes.code_id AND codes.value = ?", c]} Error Association named 'INNER JOIN cand...

activerecord-sqlserver-adapter on Ubuntu

I followed the instructions found here to get my rails app to communicate with SQL Server on Ubuntu 10.04 http://wiki.github.com/rails-sqlserver/2000-2005-adapter/platform-installation-ubuntu-2 All the tests documented there have passes except the when I try script/sconsole I was able to make a db connection in irb In my app I have a ...

Self-referential has_many :through with customized :primary key issue

I'm trying to emulate the twitter model in my Rails 2.3.8 app (ruby 1.8.7) class Connection < ActiveRecord::Base belongs_to :subject, :foreign_key => 'subject_id', :primary_key => 'user_id', :class_name => 'User' belongs_to :follower, :foreign_key => 'follower_id', :primary_key => 'user_id', :class_name => 'User' end class User < A...

How to make ActiveRecord's association collection methods private

Hi everyone, I need a way to make association collection methods (particularly the append <<) private. Here's an example: class Foo < ActiveRecord::Base has_many :bars def add_bar (bar) # does something extra first # but still also use the <<, ultimately bars.send(:<<, bar) end end Basically, i do not want any p...