activerecord

What might this be: New Active Record chainable query language built on top of relational algebra?

I saw this on a blog today, and I thought, finally! Rails will have something like HQL or Linq. Um, or not. I couldn't find anything about this. What I really want to know: will I be able to forget what the tables are called and use the object names only? Can I finally forget join syntax? I'd like to do that before I start forgetting ev...

Ruby On Rails - How to get the Object Method Caller

Let me explain my problem: I have 2 models: class User < AR::Base has_many :contacts end class Contact < AR::Base belongs_to :user belongs_to :user_contact_id, :class_name => "User", :foreign_key => "user_contact_id" # The "contact" is an ID from the table user. def self.is_contact?(user_contact_id) # CHECK IF THE RECORDS EXIST ...

Casting problem in SubSonic 3

When saving a row that has a integere primary key following exception is thrown in the VB version: 'Public member 'ChangeTypeTo' on type 'Decimal' not found.' This happens in ActiveRecord.VB file line 3406: Public Sub SetKeyValue(value As Object) Implements IActiveRecord.SetKeyValue If value IsNot Nothing AndAlso value ...

How do you programmatically inspect the sql generated by active record

What's the code to look at the sql an ActiveRecord query is going to produce. For example, Modelname.all would produce "Select * from modelname". Is there any method that gives access to that string? ...

Is there a way to make ActiveRecord write 'WHERE (a,b) in ((1,2),(3,4))' using AR finders.

Is there a way to make ActiveRecord write 'WHERE (a,b) in ((1,2),(3,4))' using AR finders. I would think Widget.find(:all, :conditions => ['(a,b) in (?)', [[1,2][3,4]]]) but the inner arrays get converted to YAML?!?! Right now I'm using find_by_sql. Is there a better way to write this? ...

How to make a complex named scope play nice with associations and other named scopes [rails]

I have the following named scope on class RentableItem < ActiveRecord::Base named_scope :available_at, lambda{ |starts_at, ends_at| { :select => "t.*", :from => "(SELECT ri.*, COALESCE(c1.start_date, '#{starts_at}') AS EarliestAvailable, COALESCE(c2.end_date, '#{ends_at}') AS LatestAvailabl...

Multiple Associations to Same Model

I have two classes that I would like to specify as follows: class Club < ActiveRecord::Base belongs_to :president, :class_name => "Person", :foreign_key => "president_id" belongs_to :vice_president, :class_name => "Person", :foreign_key => "vice_president_id" end class Person < ActiveRecord::Base has_on...

Case-insensitive search in Rails model

My product model contains some items Product.first => #<Product id: 10, name: "Blue jeans" > I'm now importing some product parameters from another dataset, but there are inconsistencies in the spelling of the names. For instance, in the other dataset, Blue jeans could be spelled Blue Jeans. I wanted to Product.find_or_create_by_na...

How can I transparently modify an ActiveRecord method in a model?

I have a model with UUIDs stored in BINARY(16) field in a MySQL table. I would like to be able to transparently convert hexadecimal uuid into binary for the setter method and back when I use the getter method. what is the 'right' way to proceed? ...

Ruby win32ole MS Access: How to find all the records updated since last export?

Hello, I am using win32ole module/library to gain access to an Access database. But I can't find as I can in Rails the created_at or updated_at columns in any of the tables in the database. I was wondering how does one finds rows that are updated, then? So I have require 'win32ole' connection = WIN32OLE.new('ADODB.Conneciton') connecito...

SubSonic 3.0 exception "'Public member 'ChangeTypeTo' on type 'Decimal' not found."

When saving a row that has an integer primary key following exception is thrown in the VB version: 'Public member 'ChangeTypeTo' on type 'Decimal' not found.' This happens in ActiveRecord.VB file : Public Sub SetKeyValue(value As Object) Implements IActiveRecord.SetKeyValue If value IsNot Nothing AndAlso value IsNot DBNull.Value T...

Rails accepts_nested_attributes_for child doesn't have parent set when validating

I'm trying to access my parent model in my child model when validating. I found something about an inverse property on the has_one, but my Rails 2.3.5 doesn't recognize it, so it must have never made it into the release. I'm not sure if it's exactly what I need though. I want to validate the child conditionally based on parent attri...

"Was dirty" plugin for ActiveRecord?

I'm working on some code that uses a lot of after_save callbacks, and I remember seeing a plugin that allows the model.changes array to persist after a call to save. It would be a great help if I could just write if body_did_change? in my after_save calls, instead of having to hack together something with a before_save filter just to se...

ActiveRecord :include not working for belongs_to with foreign_key

I have a two models set up like this: class User < ActiveRecord::Base # external_id column in database end class UserUpload < ActiveRecord::Base belongs_to :user, :primary_key => "external_id", :foreign_key => "external_user_id" end However, whenever I do upload = UserUpload.find(id, :include => :user) The sql that gets emitte...

Does ActiveRecord save a belongs_to association when saving main object?

If I have two models: class Post < ActiveRecord::Base belongs_to :user end and class User < ActiveRecord::Base has_many :posts end If I do: post = Post.new user = User.new post.user = user post.save Does the user get saved as well and the primary key properly assigned in post's user_id field? ...

Error while using `find_or_create_by` on a `has_many` `through` association

I am running in to a problem while using find_or_create_by on a has_many through association. class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role < ActiveRecord::Base # DB columns: user_id, role_id has_many :permissions has_many :users, :through => :permissions end class User has_many :...

Rails: Can you pass arguments to the after_create method?

In my application, only users with the administrator role may create new users. In the new user form, I have a select box for each of the available roles that may be assigned to new users. I am hoping to use the after_create callback method to assign the role to the user. How can I access the selected value of the select box in the afte...

how to get next ID in a table in Rails

I have a table that has following columns id store_id store_name id and store_id are always going to be same. Since I am new to rails...I had created it by mistake. But I can't afford to go back and change stuff now since I am using store_id at a lot of places. Issue 1: I am making admin screen for this table. When I try to i...

how to print out autoincrement ID in Activerecord model

I have a before_save in my model before saving the record to the database...I'd like to print out the autoincremented ID that will be inserted. My table has a column id in it. I tried before_save :printId def printId puts "ID that will be inserted is: " + self.id end this does not work... ...

Strange behaviour in ActiveRecord finder while using SQL condition IN

I have a table called inbox_messages with following rows: user_id message_id ======= ========== 4 8 4 1 4 7 0 9 0 10 0 11 0 12 The table maps to the following model: class InboxMessage < ActiveRecord::Base end When I invoke the find method with a IN caluse, I get different resultset dependi...