activerecord

How to extent activrerecord,just make id to id.to_i

module ActiveRecord module Mixin alias old_id id def id old_id.to_i end def hello "hellooooooooooooo" end end end ActiveRecord::Base.send :include, ActiveRecord::Mixin I make is because: id column in oracle is number type,not number(10), @user.id return 123.0,not 123,so I would like to do it b...

ActiveRecord find all parents that have associated children

I don't know why I can't figure this out, I think it should be fairly simple. I have two models (see below). I'm trying to come up with a named scope for SupplierCategory that would find all SupplierCategory(s) (including :suppliers) who's associated Supplier(s) are not empty. I tried a straight up join, named_scope :with_suppliers, :...

Odd ActiveRecord model dynamic initialization bug in production

I've got an ActiveRecord (2.3.5) model that occasionally exhibits incorrect behavior that appears to be related to a problem in its dynamic initialization. Here's the code: class Widget < ActiveRecord::Base extend ActiveSupport::Memoizable serialize :settings VALID_SETTINGS = %w(show_on_sale show_upcoming show_current show_past) ...

ActiveRecord Save Dependent Model

I am trying to save a model with its dependency models being saved. Model1 has_many :model2, :autosave => true Model2 belongs_to :model1 has_many :model3, :autosave => true Model3 belongs_to :model2 I want to save Model1, and have Model2 and 3 save as well. I tried this without and with the autosave feature. What win...

ActiveRecord bug? Or am I getting it wrong? (validates_presence_of if)

Ok: User attr_accessible :name, :email, :email_confirmation validates_presence_of :email_confirmation if :email_changed? What happens in the following situation: u = User.find 1 u.name = 'Fonzi' u.name_changed? # => true u.email_changed? # => false u.valid? # => false : email_confirmation is required Basically, if I change...

Mysql question: is there something like IN ALL query?

For example this query: SELECT `variants`.* FROM `variants` INNER JOIN `variant_attributes` ON variant_attributes.variant_id = variants.id WHERE (variant_attributes.id IN ('2','5')) And variant has_many variant_attributes What I actually want to do is to find which variant has BOTH variant attributes with ID = 2 and 5. Is t...

Help with Associations in Rails 3

Ruby: 1.9.2 Rails: 3.0beta3 I need some help with associations in Rails 3. I have the following models (see excerpts below): School, State, SchoolLocale The schools table has the following fields: id, name, state_id, school_locale_id The states table has the following fields: id, abbr, name The school_locales table ha...

Comparing lists of field-hashes with equivalent AR-objects.

I have a list of hashes, as such: incoming_links = [ {:title => 'blah1', :url => "http://blah.com/post/1"}, {:title => 'blah2', :url => "http://blah.com/post/2"}, {:title => 'blah3', :url => "http://blah.com/post/3"}] And an ActiveRecord model which has fields in the database with some matching rows, say: Link.all => [<Link#2 @ti...

Adding a column to a model at runtime (without additional tables) in rails

I'm trying to give admins of my web application the ability to add some new fields to a model. The model is called Artwork and i would like to add, for instante, a test_column column at runtime. I'm just teting, so i added a simple link to do it, it will be of course parametric. I managed to do it through migrations: def test_migrati...

Validate HAML from ActiveRecord: scope/controller/helpers for link_to etc?

I like HAML. So much, in fact, that in my first Rails app, which is the usual blog/CMS thing, I want to render the body of my Page model using HAML (obviously I won't do the same for Comment!). So here is app/views/pages/_body.html.haml: .entry-content= Haml::Engine.new(body, :format => :html5).render(self) ...and it works (yay, recur...

Random record in ActiveRecord

I'm in need of getting a random record from a table via ActiveRecord. I've followed the example from Jamis Buck from 2006. However, I've also come across another way via a Google search (can't attribute with a link due to new user restrictions): rand_id = rand(Model.count) rand_record = Model.first(:conditions => [ "id >= ?", rand_id...

Next, Previous Records Using Named Scope

I have a model for which I want to retrieve the next record(s) and previous record(s). I want to do this via a named_scope on the model, and also pass in as an argument the X number of next/previous records to return. For example, let's say I have 5 records: Record1 Record2 Record3 Record4 Record5 I want to be able to call Model.p...

Ruby on Rails: How to get ActiveRecord to show the next id (last + 1)?

Is there a compact way with ActiveRecord to query for what id it's going to use next if an object was going to be persisted to the database? In SQL a query like this would look something like: SELECT max(id) + 1 FROM some_table; ...

Active Record Associations: has_one :through? Or multiple has_one's?

I'm brand new to Rails, so bear with me. I have 3 models: User, Section, and Tick. Each section is created by a user. My guess with this association: class Section < ActiveRecord::Base has_one :user end Next, each user can "tick" off a section -- only once. So for each tick, I have a section_id, user_id, and timestamps. Here's ...

Using Rails Helper Methods within ActionRecord Queries?

I have a table of events (in a sqlite3 database for what it's worth) with a column called "when" that contains a timestamp detailing precisely when the event that particular row denotes is set to occur. Right now, I have @events = Event.find(:all) in my controller and I am using template helper methods to calculate where to place eac...

Rails Model and DB schema with two foreign keys from the same table

I'm developing the Order Model for a Rails application.I'm trying to represent an Order which has BillToAddressId and ShipToAddressId as the foreign keys from the Address table. The address table is below : create_table :addresses do |t| t.string :country t.string :state t.string :city t.string :zipcode ...

Using sortable_element in Rails on a list generated by a find()

Hey all, I'm trying to use the scriptaculous helper method sortable_element to implement a drag-and-drop sortable list in my Rails application. While the code for the view looks pretty simple, I'm really not quite sure what to write in the controller to update the "position" column. Here's what I've got in my view, "_show_related_pgs.e...

rails: include statement with two ON conditions

Hi, I have tree tables books bookmarks users where there is a n to m relation from books to users trough bookmarks. Im looking for a query, where I get all the books of a certain user including the bookmarks. If no bookmarks are there, there should be a null included... my sql statement looks like: SELECT * FROM `books` LEFT OU...

how to convert legacy query to ActiveRecord Rails way

I have a query in my code as below @sqladdpayment = "INSERT INTO payments (orderid, ttlprodcost, paytype, paystatus,created_at,updated_at,userid,storeid) VALUES ('" + session[:ordersid] + "', '" + session[:totalcost] + "', '" + "1"+ "', '" + "complete" +"',current_date, current_date, '"+"1"+"','"+ "1"+"')" Here the table paymen...

Position of object in database

Hi! I have got model Team and I've got (i.e.) team = Team.first :offset => 20. Now I need to get number of position of my team in db table. I can do it in ruby: Team.all.index team #=> 20 But I am sure that I can write it on SQL and it will be less expensive for me with big tables. ...