activerecord

Self-join on a table with ActiveRecord

I have an ActiveRecord called Name which contains names in various Languages. class Name < ActiveRecord::Base belongs_to :language class Language < ActiveRecord::Base has_many :names Finding names in one language is easy enough: Language.find(1).names.find(whatever) But I need to find matching pairs where both language 1 and l...

CodeIgniter/PHP Active Record won't increment an integer

Hi folks Here's my query, in CodeIgniter's Active Record: function calculate_invites($userid) { $this->db->where('id', $userid) ->update('users', array('invites' => 'invites-1', 'sentinvites' => 'sentinvites+1'), FALSE); } The fields invites and sentinvites are both integers but are set to 0 after the function is r...

Validating boolean value in Rspec and Rails

I'm pretty confused how to validate boolean values in Rspec and Rails. I understand everything except for false and nil are taken as true in Ruby. But when I use MySQL with Rails, it uses 1 for true and 0 for false (if my understanding is correct). I have the following model spec. I'd like to test boolean value for superuser attribute. ...

Searchlogic Results Duplicating with Each Association

When I chain searchlogix conditions that associate different tables, the results duplicate. When I chain searchlogix conditions querying fields from a single table, the results work perfectly. Below is an example of chaining conditions across multiple tables (machine belongs to a manufacturer and machine habtm tools and systems). Mach...

RoR: Listing of subrecords

Schema: persons (id, name, birthyear, gender) pets (id, person_id, name, leg_count) plants (id, person_id, kind, qty) I would like to make a read-only report about these things grouped by persons. The listing of personns is done (without the associated records). I would like to have "subtables" per persons. Something like: Persons +...

ActiveRecord doesn't work on one table

I have a Rails Model: ruby-1.9.2-p0 > NavItem => NavItem(id: integer, item_identifier: string, description: string, description2: string, packing_unit: string, sales_unit_of_measure: string, ean_code: string, evp_price: string, item_category_code: string, class: string, product_group_code: string, maintenance_status: string) If I wa...

On Ruby on Rails' script/console, is there a way to print out all the models in your app? (all ActiveRecord subclasses)

which is to find out all the Models in the app. This can be found out from the project folders / filenames, or in the route.rb, but just wonder if there is a Ruby way of finding it out on script/console? ...

Overwrite ActiveRecord's save method to use custom interface

Hello, I got the following situation: I have a rails application and two databases, one is for the Rails Application and the second database (running on the same DB Server Instance) is for a different non-web application. My Rails Web Application may read from the (second) Database directly via SQL but it can't write to it because of ...

Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified

My app works pretty much perfectly in production – the website part of it, at least. The problem only pops up when I SSH into my VPS and do "rails c RAILS_ENV=production". The console works fine in development mode. I've had this problem before (or at least one that looks like it) and fixed it by adding "reconnect: true" to database.yml...

How to access Mysql::Result in ActiveRecord?

Example: result = ActiveRecord::Base.connection.execute("select 'ABC'") How can I get the 'ABC' value from result? Tried result.first without success. Thanks p.s. Gems: activerecord (2.3.9) mysql (2.8.1) ...

When to add what indexes in a table in Rails

I have a question about Rails database. Should I add "index" to all the foreign keys like "xxx_id"? Should I add "index" to the automatically created "id" column? Should I add "index(unique)" to the automatically created "id" column? If I add index to two foreign keys at once (add_index (:users, [:category, :state_id]), what happens? H...

how to require active record working outside of rails

i need to require active record, but I am working outside of rails (here is why: Simple Ruby Input Validation Library). do I need to require the entire rails gem, or can i be DRYer? ...

Does anyone have a Ruby on Rails application with 500+ tables?

I am working on developing a large SAAS application and I had planned to do so in Ruby On Rails. However, most of the threads that I have read here and there on the Net seem to indicate that 100 tables is considered to be a large RoR application. I would be very interested to hear about scalability/size issues that anyone has seen and ...

Validating :inclusion in rails based on parent Model's attribute value

I have two models Project and 'Task` where project has_many tasks and task belongs to project Now in my Task model I am doing validation on a field using attributes in the project validates :effort, :inclusion => 1..(project.effort) This results in an error method_missing: undefined method project Question is, how can I validate a c...

Searching ActiveRecord timestamps using a optionally specific date

Given the following route: match "/articles/(:year/(:month/(:day)))" => "articles#index", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ } And the following set of URLs: /articles/2010 /articles/2010/09 /articles/2010/09/08 Does ActiveRecord offer any sort of built-in find method that would allow me to find ...

named_scope width has_many association and count

I have models: class User < ActiveRecord::Base has_many :user_skills end class UserSkill < ActiveRecord::Base belongs_to :user named_scope :rated, :conditions => "count_raters > 0" end I want to get count of rated skills. current_user.user_skills.rated.cont It works fine, but the generated SQL is a very strange. SELECT...

ActiveRecord to use mysql named pipes on windows

Hi Is it possible to connect from ruby/ActiveRecord to a mysql database over named pipes. OS is Windows. Thanks. ...

Better way to access individual Rails ActiveRecord error?

I'm trying to access the type attribute of an ActiveRecord::Error object. The reason I'm doing this is because I want to redirect a user to a different page depending on the type of validation that failed (an attribute can fail validation in several ways, so the attribute itself is insufficient). The only way I've found that I can do th...

Rails: run rake tasks like migrations

My dev team needs to more precisely run rake tasks. There are certain tasks that need to be only run once after a specific code change. Without getting too specific, it would be like needing to update certain existing users records after a new business rule for new users is implemented in the code. We like how migrations use a db table...

How to truncate a join table in rails?

To truncate an ActiveRecord table, I can do Category.destroy_all or Post.destroy_all How does one go about truncating a categories_post table? ...