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
-----------------------...
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:
...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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.
...
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...
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 ...
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...
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
...
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...
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...