activerecord

Rails, CookieStore vs ActiveRecordStore

Hello, I am currently experiencing a strange issue with our users being logged out. I haven't been able to reproduce it explicitly. The Rails application is using the default CookieStore. My initial hypothesis is that somehow the session data within the cookie, or even the cookie itself is being destroyed. This may be either from a use...

named_scope and HABTM association

Hi all, I have a models User class User < ActiveRecord::Base has_many :ratings has_many :rated_films, :through => :ratings, :source => :film end and Films class Film < ActiveRecord::Base has_many :users, :through => :ratings end I am looking to find all Films that have not been rated by the specified user...

[hibernate] SELECT AVG("...") with Criteria API

Hello, I'm having difficulties with translating following SQL syntax into Criteria API: SELECT AVG(dbo.Member.Points) FROM dbo.Member WHERE dbo.Member.PaidMemberRegDate IS NOT NULL; I have a Member class with a Points property. I just want to get the average Points of all Members that have the property PaidMemberRegDate set to null. ...

named_scope conditions and timezone mismatch

Here is the setup: end_date = DateTime.parse('2010-01-01 12:00:00-04') and sometimes it's initialized: end_date = 1.days.ago The question... do these named_scope(s) generate the same EXACT SQL? named_scope :before, lambda { |end_date| { :conditions => ["overdraft_transaction_payments.created_at < ?", end_date] }...

Join on polymorphic association

Hi guys I know that this is really bad idea but I want join three tables for my query on polymorphic association for example class Article has_many :comments, :as=>:commentable end class Post has_many :comments, :as=>:commentable end class Comment belongs_to :commentable, :polymorphic=>:true end and I need to get something s...

eager loading a small subset of all has_many objects based on conditions

How do I eager-load only some of the objects in a has_many relationship? Basically, I have four objects: Assignment, Problem, AssignmentUser, and ProblemUser. #assignment.rb has_many :problems has_many :assignment_users #assignment_user.rb belongs_to :assignment belongs_to :user has_many :problem_users #problem.rb belongs_to :assig...

ActiveRecord marshal dump/load has a different behavior on boolean [rails]

Step 1: [setup] a = Aritcle.new(:some_boolean => false) a.save write_to_memcache(a.id, Marshal.dump(a)) b = Marshal.load(read_from_memcache(a.id)) If I do: b.some_boolean = nil b.save the some_boolean is not written back to the DB. DB still remains false. But: a.some_boolean = nil a.save works perfectly. DB is set to NULL. A...

how to find distinct values in has_many :through

Say I have the following in my console: @user.favcolors => [#<FavColors id: 1, color_id: 18, fav_id: 1>, #<FavColors id: 2, color_id: 19, fav_id: 1>] @user.favcolors.count => 2 However, since fav_id is same in both results (1). I want this count to be 1, instead of 2. Is there a way I can put where clause in the code @user.fav...

how to pass data from view to model in codeigniter

Hi guys, I'm developing a system with codeigniter, in my situation i have to press a link in one user interface and get it's ID and pass it to the model and get the data relevant to that ID and go to the other interface and display the data in the relevant fields, i know how to pass data from model to view, but i don't know how to pass ...

How do I extract a "table" from an array of ActiveRecords in Rails?

I have several similar Models (ContactEmail, ContactLetter, ContactPostalcard). Each instance (record) in, say, ContactEmail, means a specific Email template was sent to a specific Contact. So, each one (e.g. ContactEmail) belongs_to :contact So, in ContactEmail model, there is an attribute ContactEmail.contact_id. Each Contact has a...

Is Repository pattern as same as Active Record pattern?

They seems to be similar. ...

Codeigniter active record with several like or?

Hey.. I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord): SELECT * FROM (`posts`) WHERE `city` LIKE '%test%' AND `title` LIKE '%%' OR `text` LIKE '%%' Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there t...

Ror Active Record "component" classes

Hello, coming from my experience with nHibernate, I wonder how one maps (n)hibernate's component classes in ActiveRecord class Person < ActiveRecord::Base #somehow get a :name attribute here end class Name @fist_name @last_name end how can this be done with one table only (so this is no 1:1, but i want to have a :name_first_na...

What's the correct way to inject common properties into an activerecord class?

I planned on using this module (full exmpample here http://pastie.org/1098444) puts "Name_and_key was referenced." module Name_and_key def normalize(s) s.mb_chars.normalize(:kd).gsub(/[^\-x00-\x7F]/n, '').to_s end def name=(name) self[:name] = name self[:key] = normalize(name).downcase end def name self[:na...

Is it possible to group by hour/minute/quarter hour directly in ActiveRecord/Rails?

I want to group my data by the hour/day possibly even to the quarter hour. Can I do this directly in ActiveRecord or should I just grab all the data and do the aggregation manually? Table "Checkin": Time NumBikes ChangeBikes 09:00 5 5 09:05 6 1 09:10 10 4 10:00 6 4 10:05 8 ...

How do I use the 'map' method in an ActiveRecord class method?

Not sure on my Ruby syntax here. I want to define a method that I can call like this: client.invoices.average_turnaround. So my average_turnaround method needs to work with a collection of ActiveRecord objects. Here's my code thus far: class Invoice < ActiveRecord::Base ... def self.average_turnaround return self.map(&:turnaro...

Find Stuff (but not in production)

Using the tutorial "Find Stuff (quick and Dirty)" in "Advance Rails Recipes", I can find stuff in the development environment but not in the production environment. NoMethodError: undefined method `searchable_by' I have this code in "lib/searchable.rb" module Searchable def searchable_by(*_column_names) @search_columns = [] ...

Why does ActiveRecord only sometimes provide reciprocal associations automatically?

When working with activerecord I notice that while constructing associations, the reciprocal association is provided automatically only if the reciprocal model has already been saved. If it has not yet been saved, this reciprocal association must be defined manually. I am wondering if this is intentional or a bug, since the only differ...

Any good reason to access many-to-many join table directly?

I have a Ruby on Rails application in which the code directly access many-to-many join tables directly. This makes modifying some of the code very difficult since it bypasses the normal associations and methods created via the :has_many and :has_many :through relationships. My question is simply, is this an acceptable thing to be doing ...

Friendlier RoR ActiveRecord model definition?

Simple question that used to puzzle me about Rails: Is it possible to describe a Model's structure from within the model rb file? From what I understand a model's data structure is kept within the migration, and the model.rb file is supposed to contain only the business logic. Why is it so? Why does it make more sense to migrate the d...