Hi I have a named_scope in my User model as following.
named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } }
I want to create other two named scopes which re use this one something like,
named_scope :male, lambda { by_gender('male') }
named_scope :female, lambda { by_gender('female') }
Any idea what...
I have a Post class with a vote method which creates a Vote instance
This doesn't work
def vote(options)
vote = self.votes.create(options)
return vote if vote.valid?
nil
end
This does work
def vote(options)
options[:post] = self
vote = self.votes.create(options)
return vote if vote.valid?
nil
end
Shouldn't the...
We have objects that we want to represent in stacks (think of stacking items in an MMO). There will be duplicate rows.
Let's say our owned_objects table looks like this.
user_id | object_id
1 | 27
1 | 27
3 | 46
3 | 46
5 | 59
I want the query to do
SELECT
user_id,
object_id,
count(*) AS count
FROM ...
Is there a way to create mysql triggers using Activerecord migrations ? Has anybody worked on it share your experience . Thanks
...
I'm implementing a Blog with Post and votable Comments.
When loading a Post, I want to eagerly load all votes by the current user for the Post's Comments.
Something like this (which doesn't work):
@post.comments.all(:joins => :votes, :conditions => ['votes.user_id = ?', current_user.id])
Each Comment has a method called rated_by?
d...
AS it reads I would like to use the same model connecting to different databases, something like this:
require 'rubygems'
require 'active_record'
require "active_resource"
dbs = [ 'db_one' ]
$config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
#Tables we use from 3db
class My3DB < ActiveRecord::Base
self.abstr...
I have badges (sorta like StackOverflow).
Some of them can be attached to badgeable things (e.g. a badge for >X comments on a post is attached to the post). Almost all come in multiple levels (e.g. >20, >100, >200), and you can only have one level per badgeable x badge type (= badgeset_id).
To make it easier to enforce the one-level-p...
With the launch of Amazon's Relational Database Service today and their 'enforced' maintenance windows I wondered if anyone has any solutions for handling a missing database connection in Rails.
Ideally I'd like to be able to automatically present a maintenance page to visitors if the database connection disappears (i.e. Amazon are do...
I'm building out an Answer ActiveRecord class that can have different types of answers. That is, the answer could be an integer, float, date, string... whatever...
The way I see it there are two ways to store the answers
1)
Have an attribute named "value" that is serialized.
This is nice since you can always access the answer from ...
Hello,
I have two model objects:
Event
Venue
Events have Venues. Venues can have 1..* events.
Venues have a location, a lat and long, which I use with the Geokit Rails plugin. Here's what these models look like in Rails:
class Event < ActiveRecord::Base
belongs_to :venue
acts_as_mappable :through => :venue
end
and
class Ven...
Let's say I have a form where users can search for people whose name starts with a particular name string, for example, "Mi" would find "Mike" and "Miguel". I would probably create a find statement like so:
find(:all, :conditions => ['name LIKE ?', "#{name}%"])
Let's say the form also has two optional fields, hair_color and eye_color ...
I have a small ruby script in which I'd like to use ActiveRecord to easily access a database model. What is the best way to do it?
...
I tried to define a default_scope in the following way:
default_scope :joins => :product, :select => "catalog_products.*, products.*"
What I'm getting from Rails though is this:
SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id
When I define it as a named_scope...
I am using the Classifier:Bayes as part of a model class. I have the class set up to serialize the classifier to the db.
class Foo < ActiveRecord::Base
serialize :classifier
end
The yaml appears in the db just fine after doing some training and saving the object.
But when I query for the class, instance.classifier is a string
@f...
I have code like:
@notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>"deliver_by >= '#{Date.today.to_s(:db)}'")
logger.info @upcoming_reminders[0].inspect
logger.info @upcoming_reminders[0].class
logger.info @upcoming_reminders[0].id
logger.info "--------------------------...
Hello There,
I am working on a bug(?) for a few hours now, but couln’t fix it.
This is my code:
if(!$this->db->get_where('merken',array('m_merken' => $brand))->count_all_results()){
$insetData = array('m_name' => $brand);
$this->db->insert('merken', $insetData);
}
$brand contains ‘Acer’ in this preview.
A Database ...
Hello
I have no good title for this question, but my problem is to set a conditon for my sub-table in rails
I have a model named "users" and another named "hours", hours is set to "belongs_to :users" and the users-model has "has_many :hours"
But my problem is when I try to fetch the users, but just the hours added this month. I want thi...
Hi,
Here is an outline of my app:
require 'sinatra'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql", host => $DB_HOSTNAME,
:database => $DB_NAME,:username => $DB_USERNAME,:password => $DB_PASSWORD)
class Sometable < ActiveRecord::Base
end
get '/' do
#stuff with Sometable
end
# a lot ...
Hi all,
thanks for your time first...after all the searching on google, github and here, and got more confused about the big words(partition/shard/fedorate),I figure that I have to describe the specific problem I met and ask around.
My company's databases deals with massive users and orders, so we split databases and tables in various ...
I'm using PostGis to store some spatial data. I used ActiveRecord to retrive data from db, and I used [Property (Formula = "asbinary(shape)")] on property where geometry was stored (property type was byte[]). However this doesn't work when inserting data. So I figured out to write custom insert (and update) queries to solve the problem. ...