I've got a website model that requires a user to verify ownership of the website.
Thanks to stack overflow, I was able to find the solution for user ownership verification here: http://stackoverflow.com/questions/1842416/validate-website-ownership-in-rails
After the model passes the verification test there is a verified attribute that ...
I am trying to create some cucumber features with references to other objects. I am running into some problems when I want to create relationships between objects and run tests on them. I haven't found any samples anywhere else.
Let's say I have the following models:
class Athlete < ActiveRecord::Base
belongs_to :shoe_size
validat...
Given the relations
{
proposals(id, ...),
reviewers(id, ...),
comments(id, user_id, proposal_id, ...),
votes(id, user_id, proposal_id, ...)
}
how can I create an association from Vote to Comment? Each reviewer can vote once on a proposal "create unique index on votes(user_id, proposal_id)" and can comment many times. A...
Hi. I need to query my database using mysql native functions such as YEAR(date) or NOW().... This works on staging and on production environments but it won't work in my development environment because of SQLite's lack of support to these functions.
Can you help?
Thank you.
...
Say I have the following association with an attached condition:
belongs_to :admin_user,
:class_name => 'User',
:foreign_key => :admin_user_id,
:conditions=> 'users.admin=TRUE' # or any variation with hash or array, {:admin => true}, etc.
The API doc states that the :conditions option on belongs_to will:
Specify the...
I have two tables, users and groups. A user can belong to many groups. A group can have many users.
So I have created a have_and_belongs_to_many relationship between users and groups using a join table, groups_users. This all works as expected.
What I would also like to do is specify an ACTIVE group for each user. Were it not for the h...
Hello. There are a lot of ways to store site preferences in database. But what if I need to manage datatypes. So some preferences will be boolean, others strings, others integers.
How can I organize such store?
...
I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts.
I'm trying to spec out a code block that includes something like this:
user = User.find(123)
post = user.posts.find(456)
I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of...
User model:
class User < ActiveRecord::Base
named_scope :clients,
:conditions => "roles_users.role_id = #{Role.find_by_name('client').id}"
end
When testing, throws error:
Called id for nil, which would mistakenly be 4 -- if you really wanted (etc.)
Role fixtures:
client:
name: client
user:
name: user
Apparent problem:...
Hello, I have a Topic and a Project model. I have a Many-to-many ass between them (HABTM one).
In the Topic's Index Page, I want to display the number of projects that each topic have. So I have
@topics = Topic.all(:include => [:projects])
In my controller, and so far so good. The problem is that the Project Model is so big that the ...
Hi there,
I'm creating a plugin and am having a hard time defining a before_save filter that calls an instance method I've just defined. Here's a quick sample:
module ValidatesAndFormatsPhones
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def validates_and_formats_phones(field_names = [...
The attributes method returns a hash of all the attributes with their names as keys and the values of the attributes as values; I want to utilize this method, creating a new derivative of the update_attributes(attributes) method, lets call it jz_attributes(attributes).
Update_attributes does this:
def update_attributes(attributes)
s...
I have a twitter style application whereby a user follows many people.
This is achieved using a self-referential association.
This all works very nice but my brain has just gone dead while trying to figure out the active record syntax needed to list status updates (posts) from people the user follows ordered by time in a single query.
...
I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using has many through in my models like this:
has_many :users, :through => :users_posts #Post...
Is there a method to save an object but return the object if it fails the validation_uniqueness_of for a given field? For example, I have the following:
class User
has_many :words
...
end
class Word
belongs_to :user
validates_uniqueness_of :title
...
end
And I have a situation where I want to either return the Word object if...
Please, explain me how to use Experssion.Or() in FindAll function.
for example i have 2 conditions - Expression.Like("Text", "%coolstuff%") and
Expression.Eq("FromInternet", false)
how to use them together via "or"?
...
I am trying to model a very simple system, but the Rails way of doing this is escaping me. I'd like to have two entities, a User and a Bet. A bet has two users and a user has many bets. I'd like the creating User of the bet to be able to mark it as resolved (do any form of update/delete on it).
My initial thinking was to have a User ac...
I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with #change_column or something?
...
I cannot get the composite_primary_keys gem to work with my database.
I have the following database tables:
mysql> describe currencies;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| curr_code | varchar...
I'm just diving into ActiveRecord and have not been able to find an answer to my question. If I am updating an object's attributes and then calling save()... will ActiveRecord save to the DB ONLY when the new values are different from the old values?
Let's say I do something like this:
thing_to_update = Thing.find_or_create_by_code(so...