activerecord

Move this logic into the find method?

This is working as is, but I'm pretty sure this is sloppy. Any tips on how to get all of this logic more rails-ish? I'm trying to implement will_paginate with this, but first I need a cleaner way of selecting the right records. #shoe table ------------------------------------- size | brand | color | sold -----------------------...

undefined local variable or method 'within' for User:class

Hi there! I am working with a book to teach myself Ruby-on-Rails. Ruby version is 1.2.3 and rubygems V 1.3.5. I start the console by ruby script/console and enter: user = User.new(:screen_name => "example", ?> :email => "exampleATexample.com", ?> :password => "example") but instead of adding the data to the DB, I get the following: ...

Error Handling in ActiveRecord Transactions?

I need to create a row in both tickets and users table... I just need to know how to process in case the transaction fails. @ticket.transaction do @ticket.save! @user.save! end #if (transaction succeeded) #..... #else (transaction failed) #...... #end On a side note I'd just like to thank everyone who participates at stack ov...

Complex SQL query across habtm join table with ordering in rails

I have three tables: Posts Keywordings Keywords Relevant fields in parens. A Post has_many :keywordings has_many :keywords, :through => :keywordings A Keywording(post_id, keyword_id) belongs_to :post belongs_to :keyword A Keyword(name) has_many :keywordings has_many :posts, :through => :keywordings I want to find all posts t...

Rails - setting model values to nil

Does Rail's ActiveRecord have any special rules when attempting to set one of the column values in the model to nil? The sanity check in the method freez always fails. Model: Project Column: non_project_costs_value (nullable decimal field) def froz? return !(non_project_costs_value.nil?) end def freez(val) raise 'Already fr...

What is a persistence engine (like ActiveRecord)? Advantages? Disadvantages?

Hey all, I've heard all this talk about Rail's persistence engine (ActiveRecord). I did a search on it and I couldn't really get a clear idea of what it did. It seemed like it was an Object mapping to a database but that's all I came up with. If so do they save the objects to the database to maintain persistence? What are the advantages...

Problems with installing plugins using command line on windows?

I've been trying to get GeoKit installed on my computer and for the life of me I couldn't understand why acts as mappable was showing up as an unknown variable. I followed the instructions (I'm using Windows): ruby script/plugin install git://github.com/andre/geokit-rails.git I added to the environment.rb config.gem "geokit" and I...

Avoid SHOW FIELDS in ActiveRecord

Is there any way to prevent ActiveRecord from issued a SHOW FIELDS to the database when it's not needed? I'm working on database performance critical application, and just noticed that on a typical query my SELECT takes 0.5 ms and the related SHOW FIELDS takes 2 ms -- 4 times longer! Even more importantly, it's not needed because I'm al...

Overriding attributes for nested model mass assignment

I am currently using nested model mass assignment on one of my models. It works a treat, however, I'd like to be able to ensure that any nested models that are created "belong_to" the same user. I've managed to implement this by using alias method chaining with: def contact_attributes_with_user_id=(attributes) self.contact_attrib...

Where is the right place to put _changed? methods in Rails?

I have a user model that checks to see if a value has changed before_save (by running User.zipcode_changed?). The idea is that this will queue up a delayed job if it has. Problem is, when I migrate the app from scratch I get an error: An error has occurred, all later migrations canceled: undefined method `postcode_changed?' for #<Use...

My custom destroy method does not trigger the default before and after destroy callbacks

I am writing a plugin that provides drafting for models. A delete action is a draftable action and I do not always want to delete the origin until that deletion is published. So I wrote my own destroy method to help out with this. Everything works exactly as I want things to except, custom callbacks for :before_destroy and :after_dest...

rails/activerecord search eager loaded associations

I have a simple find statement as such: m = MyModel.find(1, :include => :my_children) With m.mychildren being an Array; is there anyway to find a particular record from within the array without having to iterate over the entire thing. If I do mychildren.find(1), a new DB query is issues, which doesn't make sense, since they are all lo...

Display all active record queries

in my test mongrel server output for a page, 8 queries are listed but many more DB are counted: Query1 Query2 ... Query8 Rendered Partial1 Rendered Partial2 .. Rendered Partial40 Completed in 4754ms (View: 308, DB: 2246) | 200 OK how do I show all the queries that are running? Also, is there documentation for what the View; count repr...

Remove an association programmatically in Rails

I'm writing an extension for Spree and I want to remove an existing association. Given the following code: class Project < ActiveRecord::Base has_one :status ... end How can I remove the call to has_one :status at runtime? I want to remove the association and associated methods. ...

Validating a Model Property is Greater than Another

First, let me say I am extremely new to Rails (toyed with it a time or two but forcing myself to write a complete project with it now, started on it yesterday). I am now trying to validate that a model property (terminology?) is greater than another. This appeared to be a perfect instance for validates_numericality_of with the greater_t...

What is the recommended way to use Subsonic

I like the simplicity of the Simple Repository , this looks ideal for simple CRUD operations. However, if I have a requirement for a complex query on top and ideally want my app to call a Stored Proc what is the recommended way to do this? Does ActiveRecord cater for Stored Procs? I will be using this in a ASP.NET MVC app and really ...

Generating XML for ActiveRecord associations using batched find

I have a controller that returns XML for a has_many association in the most straight-forward but inefficient way possible: @atoms = @molecule.atoms.find(:all, :include => :neutrons) render :xml => @atoms.to_xml(:root => 'atoms') This fetches and instantiates all objects at once. To make this more memory efficient I'd like to use Act...

ActiveRecord derived attribute persistence which depends on id value

How do you persist a derived attribute which depends on the value of id in rails? The snippet below seems to work-- Is there a better rails way? class Model < ActiveRecord::Base .... def save super #derived_attr column exists in DB self.derived_attr = compute_attr(self.id) super end end ...

How to work with join data in codeigniter?

Hi, I am having a play around with codeigniter and trying to get my head around the active record system and such like. I have set up a couple of tables and am attempting to run a join on them, as such: function GetOrganisationsAndBuildingDetails() { $this->db->select('organisations.organisation_name, organisations.organisatio...

In Rails, how can I return a set of records based on a count of items in a relation OR criteria about the relation?

I'm writing a Rails app in which I have two models: a Machine model and a MachineUpdate model. The Machine model has many MachineUpdates. The MachineUpdate has a date/time field. I'm trying to retrieve all Machine records that have the following criteria: The Machine model has not had a MachineUpdate within the last 2 weeks, OR The Mac...