activerecord

Cloned ActiveRecord object sends null id to Postgres during save

I need to save a copy of an ActiveRecord object so I'm using something like: @original = Model.find(params[:id]) @copy = @original.clone However, when I attempt to save this to Postgres: PGError: ERROR: null value in column "id" violates not-null constraint In the console: @copy.id => nil How do ...

Manage data structures in the database or in code?

I've always had a difficult time deciding where the best place is to manage my "master" set of data structures for my data access layer. Traditionally, I've always started with a database and used a code generation tool to get a set of code models (i.e. ActiveRecord in SubSonic). Lately, however, I've been attracted by the idea of mana...

RoR: "belongs_to_many"? Association headache...

I can't seem to wrap my head around this, so I thought I'd post and see if anyone could help me out (please pardon the question if it's insultingly simple: it's complicated to me right now!) I have these models: order service customer I think they speak for themselves: a service is what the customer buys when they place an order. O...

ruby on rails: Store data for current_user on different models

I am trying to store data from the current_user in a User model with attr_accessor :offset. I would like to fetch records of a different model using the offset stored in the current_user model. When I change this offset from a different model it does not save it? I feel a database Column for the offest is overkill. This offset would be ...

Can this relationship be described in Ruby on Rails?

Is this a relationship that can be described in Ruby on Rails' ActiveRecord model relationships? Customer Address =================== ========= Billing_Address_Id >------} }---|- AddressId Shipping_Address_Id >------} So that I could have data that looks...

Set default random column value in ActiveRecord model

Hi, little help needed got 2 models: class User < ActiveRecord::Base has_many :posts end and class Post < ActiveRecord::Base belongs_to :user end the Posts table has a column: u_hash. This is supposed to be a randomly generated identifying hash (for public viewing). What is the best way to generate this hash and how can I add ...

Problem find joined table in rails

I have model represent association rule (Body => Head) def Item has_many :heads has_many :bodies ... end def Rule has_many :heads has_many :bodies ... end def Body belongs_to :item belongs_to :rule ... end def Head belongs_to :item belongs_to :rule ... end I want to find rule that have body's item matched items specified and want t...

Geokit in Ruby on Rails, problem with acts_as_mappable

Hi, i have looked through the list of related questions however my problem does not seem to be listed and hence here it is: Basically I'm trying to use Geokit within the Ruby on Rails environment, im not sure if i installed it properly, I have included it within the environment.rb (and done rake db:install) and i'm now trying to do the ...

What is an active record?

What exactly is an active record? I hear this term used in Ruby post and nhibernate. ...

Dynamic Table in Model

Is it possible to make a model that uses a different table dependent on an association. ...

Using many-to-many nested models in Rails

I've got the following situation setup to model a Customer with multiple addresses and a reference table for the type of address. The data model is Customer - Address : many to many relationship, represented with a join table called 'Location'. LocationType - Location : one to many, so that a locationtype (eg 'work', 'home') can have ...

RoR: How to Change fields on Join statement generated by Active Record's sum function.

I'm doing a sum using the Sum function provided by RubyOnRails' Active Record as follows: s=DatosMateria.sum('inscritos',:conditions=> "datos_materia.ano=2005 AND materias.codigo=2394",:include=>"materias") it returns 0 and generates me the following SQL statement: SELECT sum('datos_materia'.inscritos) AS sum_inscritos FROM 'datos_m...

Rspec: undefined method `new' error in ActiveRecord model

I know this has to be something stupid, but I keep getting the following error in one of my examples: undefined method `new' for #<Class:0x211d274> I have created a simple example to show the error: describe LateCharge do before :each do @membership = Membership.new @location = mock_model(Location, :late_payment_rate => 10)...

Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ASC; Where this gets hung up is the 'rad < 5', because according to codeigniter there is no 'rad' column. I've tried writing this as a cust...

Using includes / joins

I have a Proposal model and a Signature model. Proposal has many signatures and Signature belongs to proposal My relationships work fine when I select a single Proposal, but when I do a Proposal.search and return multiple proposals, I can't access signatures. I want to sort my proposals by signatures count, so I tried this: Proposal....

hash['key'] to hash.key in Ruby

I have a a hash foo = {'bar'=>'baz'} I would like to call foo.bar #=> 'baz' My motivation is rewriting an activerecord query into a raw sql query (using Model#find_by_sql). This returns a hash with the SELECT clause values as keys. However, my existing code relies on object.method dot notation. I'd like to do minimal code rewrite. Th...

Database performance with Nhibernate and Activerecord

Inspired by the DDD hype, I designed my classes pretending there was no database at all. And then used NHibernate to map the classes to database tables. Part of my class graph look like this: Order (hasmany)--> Product (belongsto)-->Seller. To retrieve all orders placed for a certain seller. I have the code: public class Order:ActiveRec...

Reload column names in ActiveRecord model class

I have a script using ActiveRecord that creates column names dynamically based on values read from a CSV file, something like this: FasterCSV.foreach('votes.csv', :headers => true) do |row| column_name = "roll_call_id_#{row['roll_call_id']}" if !Legislator.columns.map(&:name).include?(column_name) connection_pool.connection.add...

Load Ruby on Rails models without loading the entire framework

I'm looking to create a custom daemon that will run various database tasks such as delaying mailings and user notifications (each notice is a separate row in the notifications table). I don't want to use script/runner or rake to do these tasks because it is possible that some of the tasks only require the create of one or two database ro...

Filtering ActiveRecord queries by multiple unique attributes and combining results

In my rails project, I have a query which finds the 10 most recent contests and orders by their associated poll dates: @contests = Contest.find( :all, :limit => "10", :include => :polls, :order => "polls.start_date DESC" ) Currently this shows each contest and then iterates through associated polls sorting the master list by poll s...