In my Rails app, I have a class with a has_many relationship. For the sake of efficiency, I want to do some direct SQL to update many rows in the database at once, and then I want to mark the has_many relationship as no longer valid. If later code accesses the has_many relationship, I want it to reload the data. But I obviously want t...
Hi Guys,
i'm using these two lines to update my table using codeigninter active records
$this->db->where('reference_number', $reference);
$this->db->update('patient', $data);
what i want to de is to check weather it successfully updates the table and according to that i want to give a notification to the user, how can i check the su...
I have a Coach Model which:
has_many :qualifications
I want to find all coaches whose some attribute_id is nil and they have some qualifications. Something which is like.
def requirement
legal_coaches = []
coaches = find_all_by_attribute_id(nil)
coaches.each do |coach|
legal_coaches << coach if coach.qualification...
Hi everyone,
I try to figure out how I can localize the error item name in my rails application which appear when a user sign's up with uncorrect datas... I figured out how to override the messages but not the names of the messages like ("password", "login", "email", ...)
de:
activerecord:
errors:
models:
user:
...
Hi,
I want to perform an operation on a array returned from an ActiveRecord query. This is the functionality I would have done on the ActiveRecord directly.
Modification.find(:all, :group=>'ref_id,start_date', :order=>'updated_at desc')
The above is working fine as expected. But the problem is I cannot perform it directly for some re...
I'm working on a multi-site CMS that has a notion of cross-publication among sites. Several types of content (Articles, Events, Bios, etc) can be associated with many Sites and Sites can have many pieces of content. The many-to-many association between content pieces and sites must also support a couple common attributes for each conte...
Hi, while writing some application for personal use. I find out the child query is not as great as it look.
For instance, I have 2 object
Category has_many Files
File belongs_to Category
File.category will access its parent category. But this lead to the famous N+1 problem.
For example, I want my homepage to list out 20 newest file...
I'm using Codeigniters Active record library to carry out an update on a column on my DB.
Here's the SQL for the table
CREATE TABLE `schedules` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`reservation_id` INT(11) NULL DEFAULT NULL,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(512) NULL DEFAULT NULL,
`start_date` DATE NOT NULL,
`st...
I usually use a php class to create value objects for my database objects. Something like:
class UserVO
{
public $id;
public $email;
}
and I then return this as follows:
$data = mysql_query($sql);
while ($row = mysql_fetch_object($data, "UserVO"))
{
$result[] = $row;
}
return $result;
I’m now trying to get to grips w...
Trying to retrieve an array of ActiveRecord Objects grouped by date with PostgreSQL.
More specifically I'm trying to translate the following MySQL querry:
@posts = Post.all(:group => "date(date)",
:conditions => ["location_id = ? and published = ?", @location.id, true],
:order => "created_at DESC")
I am aware that PostgreSQL ...
When I run the mongrel server in a command window everything works
fine, database connections are made and the app is running properly.
If I set the Windows Service to run my app by logged in with my user
credentials it works fine as well. However, when I set the Service to
run as Local System I cannot get the application to start an...
It's very simple, I want to handle a normal [show] request with a call to DataMapper like I did in Merb.
With ActiveRecord I could have done this:
class PostsController
def show
@post = Post.get(params[:id])
@comments = @post.comments unless @post.nil?
end
end
and it handles the 404 by catching the resource's exceptions.
...
I develop Sinatra application and use there ActiveRecord for working with database, but I encountered one problem. I wrote a test for a model and it breaks with
SQLite3::CantOpenException: unable to open database file
Connection to database is established in test_helper.rb with the following code:
Dir.chdir('..') do
ActiveRecord::Ba...
I'm a beginner at rails. And I've come to understand two different ways to return the same result.
What is the difference between these two? And what situation would require you to choose one from the other?
Example 1:
Object.find(:all).select {|c| c.name == "Foobar" }.size
Example 2:
Object.count(:conditions => ['name = ?', 'Fooba...
Is there a better way of writing this? Is it possible to do cleanly in one line?
conditions = ["category = ?", params[:category]] if params[:category]
@events = CalendarEvent.all( :conditions => conditions )
...
In Rails while using activeRecord why are join queries considered bad.
For example
Here i'm trying to find the number of companies that belong to a certain category.
class Company ActiveRecord::Base
has_one :company_profile
end
Finding the number of Company for a particular category_id
number_of_companies = Company.fin...
I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers.
The problem is, that join table entries are deleted, not destroyed. If relation is gone, I have ...
What i'm looking to do is have a base class for some of my models that has some default activerecord behavior:
class Option < ActiveRecord::Base
has_many :products
def some_method
#stuff
end
#etc..etc..
end
class ColorOption < Option
#stuff...
end
class FabricOption < Option
#stuff...
end
However, I want ColorOpti...
What is the recommended approach for finding multiple, unique associated models for a subset of another model? As an example, for a subset of users, determine unique artist models they have favorited.
One approach is to grab the users from the database, then iterate them all quering for favorites and building a unique array, but this se...
With Rails/ActiveRecord 2.3.8 I'd like to do:
AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' )
But AR insists on adding "()" to the generated SQL even though the field list is blank since the table DDL is being cloned, thus resulting in e.g.:
ActiveRecord::StatementInvalid: M...