I have an Event model, which stores an event feed for each user. I also need to email the updates to users which have enabled email notifications in their profile.
From an architectural point of view, which one is better?
call the mailer in an after_create method in the model, since it's a part of the business logic;
call the mailer i...
Class A and B are identical:
class A < ActiveRecord::Base
def foo
puts "foo"
end
end
class B < ActiveRecord::Base
def foo
puts "foo"
end
end
What's the difference between refactoring like this with a base class:
class Base < ActiveRecord::Base
def foo
puts "foo"
end
end
class A < Base
end
class B < Base
end
versus li...
I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....:
class Game < ActiveRecord::Base
has_many :game_users, :dependent => :destroy
has_many :users, :through => :game_users, :...
I'm writing a project at the moment in Ruby which makes use of the ActiveRecord gem for database interaction and I'm trying to log all the database activity using the ActiveRecord::Base.logger attribute with the following code
ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))
This works fine for migrations et...
While playing around with one-to-one associations in castle activerecord I stumbled upon the following problem:
I'm trying to model a one-to-one relationship (user-userprofile in this case). I already learned that this may not be a best practice, but let's ignore that for a moment (I'm still trying to understand what's going on).
[Acti...
If I write this, everything works fine:
class A < ActiveRecord::Base
acts_as_taggable
end
But if I take acts_as_taggable and put it into a module that class A includes, I get an error:
module B
def self.included(base)
base.class_eval do
extend ClassMethods
include InstanceMethods
end
end
module ClassMethods...
I have the following line in my ActiveRecord model:
class Record < ActiveRecord::Base
has_many :users, :through => :record_users, :uniq => true, :order => "record_users.index ASC"
This is intended to enable me to read out record.users in a way that I order using an index field in the record_users model.
The problem is that this f...
I've just been reading this question which is about giving an ActiveRecord model's date field a default value. The accepted answer shows how to set the default value from within the controller. To my mind, this sort of business logic really belongs in the model itself.
Then I got to thinking how if this were Java I'd probably set the in...
Is there a way to do this in Rails:
I have an activerecord query
@posts = Post.find_by_id(10)
Anytime the query is called, SQL is generated and executed at the DB that looks like this
SELECT * FROM 'posts' WHERE id = 10
This happens every time the AR query is executed. Similarly with a helper method like this
<%= f.textarea :name...
Hi. I´m using subsonic 3. I´m need pass a parent object with his childs to a custom method before they are persisted , like this:
Public Function ValidateParent(parent as Parent) as Boolean
For Each child in parent.Childs
ValidateChild(child)
Next
Return true
End Function
I'm trying to set the Parent.c...
I am still fairly new to rails and activerecord, so please excuse any oversights.
I have 3 models that I'm trying to tie together (and a 4th to actually do the tying) to create a permission scheme using user-defined roles.
class User < ActiveRecord::Base
has_many :user_projects
has_many :projects, :through => :user_projects
has_m...
I am trying to run an active record migration but am receiving the following error:
undefined method 'info' for nil:NilClass
Here is the 2 lines of code in my rake task that runs the migration
ActiveRecord::Base.establish_connection(YAML::load(File.open('src/SupporterSync.Core/Database/Database.yml')))
ActiveRecord::Migrator.migra...
I have been hearing a lot of buzz around has_many :through(HMT) vs has_and_belongs_to_many (HABTM). This post covers most of its benefits. Okay, HMT sure has some real good benefits over HABTM. However, what I wanted to know that when should I know that I should be using HMT over HABTM? Often I am in a situation where I believe that plai...
If I am not wrong then Rails has its own foreign key logic implemented using ActiveRecord. Is that to help performance i.e. so that you don't rely on the database for the additional processing logic or make frequent database transactions? Or is it for some other reason?
...
Hi,
Is it possible to use delegate in your Active Record model and use conditions like :if on it?
class User < ActiveRecord::Base
delegate :company, :to => :master, :if => :has_master?
belongs_to :master, :class_name => "User"
def has_master?
master.present?
end
end
Thnx!
...
I am currently building a rails application based around insurance. There is an admin section where the staff can control the system tables, mostly what appears in drop down lists, although some are more complex with their own associations. The main model in this application is the policy, this needs to hold/link to information in many o...
If I have a Model like the following example
class Person < ActiveRecord::Base
has_many :moods
end
class Mood <ActiveRecord::Base
end
how do I change the new.html.erb allow me to choose a mood when entering a new Person? Do I need to add the foreign keys to mysql manually?
...
Today we had a lot more activity than normal between our Rails app and our remote legacy MS-SQL Server 2005 database, and we started getting the error below intermittently. Any ideas what it is + how to prevent (besides avoiding the situation, which we're working on :)
Error Message:
ActiveRecord::StatementInvalid: DBI::DatabaseError: ...
I have been trying to track down some sample applications that are using Castle ActiveRecord.
Do you know of any good sample applications?
...
Hi all!
I have a Model called Section which has many articles (Article). These articles are versioned (a column named version stores their version no.) and I want the freshest to be retrieved.
The SQL query which would retrieve all articles from section_id 2 is:
SELECT * FROM `articles`
WHERE `section_id`=2
AND `version` IN
(
SELEC...