activerecord

ActiveRecord: Can objects be added to a has_many :through in a single batch operation?

Take the following simple object model for example: class Course has_many :enrollments has_many :students, :through => :enrollments, :after_add => :send_email def send_email(student) puts "Email Sent" end end class Enrollment belongs_to :course belongs_to :student end class Student has_many :enrollments has_many :...

Creating a created_by column and association with rails?

Sigh... I feel like a big newbie on this one, so lets say I have a few models: class Question < ActiveRecord::Base has_many :answers belongs_to :user end class Answer < ActiveRecord::Base belongs_to :question has_one :user end class User < ActiveRecord::Base has_many :questions has_many :answers, :through => :questions end...

Rails idiom to avoid duplicates in has_many :through

I have a standard many-to-many relationship between users and roles in my Rails app: class User < ActiveRecord::Base has_many :user_roles has_many :roles, :through => :user_roles end I want to make sure that a user can only be assigned any role once. Any attempt to insert a duplicate should ignore the request, not throw an error ...

Geokit and Authlogic, geocoding the users ip address when they create a user

Has anyone done this? I'm confused at how I can make this work, first off I have my user model Geocoding with it works fine in IRB, just can't figure out how to get it to work in my project. Trying to use some examples from the readme here: http://github.com/andre/geokit-rails/tree/master. Anyhow here is what I have: class User < Act...

Compile error using SubSonic ActiveRecord templates in Visual Web Developer Express

Thanks to ranomore I was able to get the Subsonic T4 code generation to work in Visual Web Developer Express for my ASP.Net MVC project, however only using the LinqTemplates. When I use the ActiveRecord templates with the same settings my project does generate files, but the code doesn't compile anymore... Any ideas? ...

Problem about ActiveRecord Templates for SQLite

I try to using the SQLite in SubSonic.Examples.ActiveRecord project so: Add a reference for System.Data.SQLite Copy the SQLite.ttinclude and Settings.ttinclude from \T4 Templates\TemplateProviders\ to the Models_Generated\ Change the <#@ include file="SQLite.ttinclude" #> in the SQLite.ttinclude and Settings.ttinclude Change the settin...

SubSonic 3 ActiveRecord Issues - Blank Data

When I examine the .Columns property of one of my business entities I had missing values for Table and PropertyName. I get the right count of records back from things like Take(5) but all 5 objects will be full of empty strings and 0 values. Just tried it with another SQL connection and same thing? Where should I start troubleshooting t...

missing attribute in activerecord

I have models Foo and Bar. Bar has column foo_id. When I call Bar.foo_id I get the error missing attribute: foo_id Keep in mind that this is not an undefined method error and the column definitely is in the db. What are some common causes of this? Thanks ...

Rails `link_to` method posting multiple times

[see later answer for more] I think this is just a simple rails question, and mostly on how i've named my models and reference them in my view. So a little background, I'm using the vote_fu plugin, which I think is great, however I'm having a hard time getting the voting to work from a link as follows: <%= link_to "vote for", current_u...

Adding find condition to all Active Record Models in Rails

Is there anyway to add a find condition to all Active record models? that is I would like this query ExampleModel.find :all, :conditions=> ["status = ?", "active"] to behave the same way as ExampleModel.find :all in every model Thanks!! ...

Joins across more than two tables with activerecord

A while ago I asked (Joins across multiple tables with ActiveRecord with named scopes) http://stackoverflow.com/questions/1189086/joins-across-multiple-tables-with-activerecord-with-named-scopes Now I need to create a named scope that involved joining across more than two tables, eg: named_scope :baz_category, lambda {|c| {:joins=>([:f...

How to render custom field with to_xml?

Hi, Everyone I am working on a custom accessor method like an example below: class Forest < ActiveRecord : Base has_many :trees def total_water_usage # summarize each tree's water_usage in this forest. # return as string end end class Tree < ActiveRecord : Base belongs_to :forest end That is, I need your help for...

Using attributes in Model.calculate method?

OK So I've got my models like so: Team has_many :users, :through => memberships User has_one :user_status Now in my Team model I want to be able to find the maximum 'updated_at' value of my 'user_status' models. I tried doing something like: Team has_many :users, :through => :memberships has_many :user_statuses, :through =>...

SQLite Int64 vs Int32 Problem and SubSonic ActiveRecord

I thought this was covered elsewhere but I don't see it now. Anyway, having a problem with a simple v3 query. Using SQLite ADO.NET provider 1.0.65.0. My table structure looks like this: CREATE TABLE "SamplerData" ("RowId" INT PRIMARY KEY NOT NULL ,"SampName" VARCHAR(128),"SampPurpose" VARCHAR(2048),"ActiveState" INTEGER NOT NULL DEFAU...

How to store ruport table data object in a database?

I intend to store a nightly build of report data in a DB and then use formatters when a user requests for a report in real-time. A few clues... The Ruport::Data::Table object - that contains the data - is a collection of Ruport::Data::Record objects, accessible via the 'data' attribute i.e. Ruport::Data::Table object *=> Ruport::Data:...

How to skip ActiveRecord callbacks?

I have model like this class Vote < ActiveRecord::Base after_save :add_points_to_user ..... end Is it possible to somehow force model to skip calling add_points_to_user when saved? Possibly something like ActiveRecord#delete vs ActiveRecord#destroy? ...

Load Binary Data From a File via ActiveRecord Create

Is there an easy (or generally accepted) way to load up a binary column using the create method of ActiveRecord? For example, what I'm trying to do is something similar to this: MyTableObject.create :name => "Test", :image => File.read("PathToMyFile.jpg") ...

Ruby on rails connection problem

I have a Ruby on Rails project that I was developing on a hosted server but have decided to work on my local windows machine with. To get started I thought I'd make sure that I could just take my models from the old project and put them in a new project then query them in the console. This fails. Edit to reflect more accurate problem:...

How can I see the SQL that will be generated by a given ActiveRecord query in Ruby on Rails

I would like to see the SQL statement that a given ActiveRecord Query will generate. I recognize I can get this information from the log after the query has been issued, but I'm wondering if there is a method that can be called on and ActiveRecord Query. For example: SampleModel.find(:all, :select => "DISTINCT(*)", :conditions => ["`d...

How access ActiveRecord::AutosaveAssociation.marked_for_destruction? from within parent model

I have a model, Person, with the following association: has_many :distributions accepts_nested_attributes_for :distributions, :allow_destroy => true validate :distributions_must_total_100 The custom validation currently fails when it shouldn't -- when some of the validations have been marked for destruction -- because they still sho...