activerecord

rails howto compare datetime ?

hello, i have games in my sqLite DB with the attribute starting_date( t.date :starting_date). i would like to know all the games that have alreday started so i am using this lines of code: Game.find :all,:conditions=>"starting_date <= #{Date.today}" Game.find_by_sql("SELECT * FROM "games" WHERE (created_at < 2010-05-13)") the resu...

How can I create activerecord models that are subsets of a certain model?

I've seen this before but can't figure out what the correct term is. Basically, I'd like to create models for a specific subset of table data. E.g. (these are not the real classes) class Person < ActiveRecord::Base class Man < Person <something here> :gender => 'male' ...

How do I aggregate activerecord model data for a specific time period?

I'm collecting data from a system every ~10s (this time difference varies due to communication time with networked devices). I'd like to calculate averages and sums of the stored values for this activerecord model on a daily basis. All records are stored in UTC. What's the correct way to sum and average values for, e.g., the previous ...

Help me understand Rails eager loading

I'm a little confused as to the mechanics of eager loading in active record. Lets say a Book model has many Pages and I fetch a book using this query: @book = Book.find book_id, :include => :pages Now this where I'm confused. My understanding is that @book.pages is already loaded and won't execute another query. But suppose I want to ...

[Rails] Accessing error_messages on form_tag

I have built a custom form for creating a joining model on a has_many :through relationship. The models look roughly like this: class Team has_many :team_members has_many :members, :through => :team_members end class Member has_many :team_members has_many :teams, :through => :team_members end class TeamMember belongs_to :tea...

named_scope + average is causing the table to be specified more then once in the sql query run on postgresql

I have a named scopes like so... named_scope :gender, lambda { |gender| { :joins => {:survey_session => :profile }, :conditions => { :survey_sessions => { :profiles => { :gender => gender } } } } } and when I call it everything works fine. I also have this average method I call... Answer.average(:rating, :include => {:survey_sessi...

Rails attribute alias

Hi, I was just wondering if it's possible to "rename" an association in Rails. Let's assume : # An ActiveRecord Class named SomeModelASubModel (some_model_a_sub_model.rb) class SomeModelASubModel < ActiveRecord::Base has_many :some_model_a_sub_model_items end # An ActiveRecord Class named SomeModelASubModelItem (some_model_a_sub_mod...

Multithreaded ActiveRecord requests in rspec

I'm trying to recreate a race condition in a test, so I can try out some solutions. I find that in the threads I create in my test, ActiveRecord always returns 0 for counts and nil for finds. For example, with 3 rows in the table "foos": it "whatever" do puts Foo.count 5.times do Thread.new do puts Foo.count ...

Ruby ActiveRecord + Mongrel going slow

I have a class like this: class Router :: Mongrel::HttpHandler def process(req, res) status, header, body = [200, {"Content-type"=>"text/html"}, Model.all.to_xml] res.start(status) do |head, out| header.each_pair { |key, value| head[key] = value } out.write body end end end It's an server and I use...

Migrate existing ROR app to GAE

I have managed to run a basic rails app1 on App Engine using: http://gist.github.com/268192 So, on my basic app2, I install CE, which works fine on local machine. (communityengine.org) But, when I follow the same steps on my actual app2, where community_engine plugin is installed and all the gems are frozen, the app engine installer sc...

rails large amount of data in single insert activerecord gave out

So I have I think around 36,000 just to be safe, a number I wouldn't think was too large for a modern sql database like mysql. Each record has just two attributes. So I do: so I collected them into one single insert statement sql = "INSERT INTO tasks (attrib_a, attrib_b) VALUES (c1,d1),(c2,d2),(c3,d3)...(c36000,d36000);" ActiveRecord:...

Fetch a Rails ActiveRecord 'datetime' attribute as a DateTime object.

I have an attribute in one of my models that contains a Date/Time value, and is declared using t.datetime :ended_on in my migrations. When I fetch this value using myevent.ended_on, I get a Time object. The problem is that when I try to use this attribute as an axis in a Flotilla chart, it doesn't work properly because Flotilla only rec...

Using active record migrations on a .NET project

I want to use the rails like database migrations on a .net project which uses an oracle database. Looking around at some of the frameworks like migratior.net (which apparently isn't well tested with oracle) I've decided to just try and use the rails active record as it looks like it's probably the most supported framework around. My qu...

Overriding an ActiveRecord attribute

I have a model with a completed:boolean column that I'd like override so I can add some conditional code. I've never override an ActiveRecord attribute before and wanted to know if the method below is good practice? class Article < ActiveRecord::Base def completed=(b) write_attribute(:completed, b) # IF b is true then do so...

Subsonic 3 foreign key (relationship) keep changing the appended number

Hi all, I have couple foreign key relationships in my tables, where multiple keys reference to the same primary from a different table. Whenever I try to run the "Run Custom Tool" when I make changes/add/delete new table, from time to time, the generated class append a different number. For example, at the moment, I have this generated ...

How to include a module in all of Rails project's ActiveRecord models?

Hi Stackies, I want to include this module in every ActiveRecord model in my Rails app, without dropping include NotificationResourceableTraits in each file. Is there a way? module NotificationResourceableTraits def self.included(base) base.has_many :notification_resources, :as => :notification_resourceable base.has_many :not...

How to connect to SQL Server using activerecord, JDBC, JTDS and Integrated Security

As per the above, I've tried: establish_connection(:adapter => "jdbcmssql", :url => "jdbc:jtds:sqlserver://myserver:1433/mydatabase;domain='mynetwork';", :username => 'user', :password=>'pass' ) establish_connection(:adapter => "jdbcmssql", :url => 'jdbc:jtds:sqlserver://myserver:1433/mydatabase;domain="mynetwork";user="mynetwork\user...

Pre-done SQLs to be converted to Rails' style moduls

I am a Rails newbie and would really appreciate if someone converted these SQLs to complete modules for rails. I know its a lot to ask but I can't just use find_by_sql for all of them. Or can I? These are the SQLs (they run on MS-SQL): SELECT STANJA_NA_DAN_POSTAVKA.STA_ID, STP_DATE, STP_TIME, STA_OPIS, S...

is using has_and_belongs_to_many for models is a bad idea?

Can some one please explain the the pros and cons between has_many :through and has_and_belongs_to_many? ...

Why is Model.scoped generating invalid SQL?

When I run Book.scoped({:conditions => ['books.index LIKE ?','%query%']}) I get: ActiveRecord::StatementInvalid: SQLite3::SQLException: near "index": syntax error: SELECT * FROM "books" WHERE (books.index like '%query%') What am I doing wrong? ...