I love making named scopes for rails. however, I ran into somewhat of a pickle.
Ive gotten pretty comfortable using named scopes for joins like so:
named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'"
Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something l...
this is what I mean:
job has many docs. I want to create a doc, I can do:
@doc = Doc.new(params[:doc])
but I'd like to enforce the parent-child relationship, since I already know the job.. something like this:
@job.docs.new(params[:doc])
so that the job_id field gets ignored and only the @job object matters...
does it make any se...
In a Rails app, I have foreign key constraints in MySQL which I set them all up manually, separate from my migrations.
I'm trying to figure out whether I should use ActiveRecord's :dependent => :destroy option. For example, in my schema I have tables...
users
-----
log_entries
-----------
user_id # Has FK constraint to users.id with ...
Guys,
I've been reading both ddd and PoEAA books, and searching a lot on the web but i'm still confused and i think there is something i'm missing about the repository pattern:
Context of use: Web apps (monorail + activerecord)
What i'm looking for:
reuse a 'base' repository among project. The repository will be the typical IReposit...
Can anyone share a few guides, tips or links to what constitute best practices for creating a model that represents a contact form which is not backed up by a table in the database, but can still validate the data entered?
Thanks.
EDIT: I forgot to mention, that if it is a gem or a plug-in, it should work with Ruby 1.9.1 on Rails 2.3.2...
I've been looking all over, and really just need a definitive answer to ride on. I'm going to have an object that contains percentages compiled from other objects, the point here is, what's the standard for representing a percentage in rails, without it getting broken in data migrations and things like that.
...
So I can do Post.delete_all to delete all my posts, but what if I want to delete all posts, comments, blogs, etc. I.e., how do I iterate over all my models and run the delete_all method?
...
Should I store latitude and longitude as strings or floats (or something else)?
(I'm using activerecord / ruby on rails, if that matters).
...
I set out to solve a problem we have, where under certain circumstances; we may need to make an attribute or relationship immutable, that is once the attribute is written it is written forever. attr_readonly wasn't appropriate, as that didn't work for relationships, so I tried to prove the concept here:
http://pastie.org/562329
This sh...
Hi,
I have two ActiveRecord models with a hasMany / belongsTo association:
class User < ActiveRecord::Base
has_many :letters
end
class Letter < ActiveRecord::Base
belongs_to :user
end
The User model has a revision_number attribute, to which I would like to scope the belongs_to association, so the letter is associated to a User b...
I have a weird situation involving the need of a double inner join. I have tried the query I need, I just don't know how to make rails do it.
The Data
Account (has_many :sites)
Site (habtm :users, belongs_to :account)
User (habtm :sites)
Ignore that they are habtm or whatever, I can make them habtm or has_many :through.
I want to b...
I've got a table with employees (id, name, role) and a relations table bosses (employee_id, superior_id; both foreign_keys to employees.id to employees).
Now if a employee logs in, I only want to show his/her employees; an admin (role=admin) can see all employees.
For the admin it's easy:
Employee.find(:all) #to list them
Employee.fin...
Hello, everyone. I have an idea to use Activerecord to implement something strange like the example below:
SystemInfo < ActiveRecord::Base
belongs_to :SystemInfo
end
The idea is, System A can contain System B as its child. So I will generate application's skeleton as:
script/generate scaffold SystemInfo parent_id:integer name:str...
Since has_one doesn't provide a before_add callback to allow validation,
how do I prevent rails from destroying the old association even when the
new one does'nt pass validation?
susan :has_one :shirt
shirt :belongs_to :susan
susan.shirt = a_nice_shirt
this destroys whatever association was present beforehand,
even if the new shirt is...
This is more of a "why do things work this way" question rather than a "I don't know how to do this" question...
So the gospel on pulling associated records that you know you're going to use is to use :include because you'll get a join and avoid a whole bunch of extra queries:
Post.all(:include => :comments)
However when you look at ...
I have a collection of ActiveRecord objects. I want to be able to run all the validations for each of these objects without actually saving them to the database. I just want to know if they would be valid were I to save them to the database. In other words, I essentially want to populate the errors data structure for each of my objects...
I'm trying to load some Rake Fixtures (rake db:fixtures:load) into a MySql database and I'm seeing some weird behaviour with AutoIncrement values. Normally this goes up by 1 for each insert which allows me to define/create tests. (BTW - normal create/insert from script works correctly).
However when I load from fixtures the id field is ...
I have a class that inherits ActiveRecordValidationBase that contains the following property:
[Property]
[ValidateDecimal]
public Decimal UnitCost { get; set; }
I also have a UnitCostTextBox that accepts input for said UnitCost.
What I would like to do is perform validation once using Castle's validators. However, it seems that befo...
Hello,
I've been struggling with this for a while, and decided to throw it out there:
I have 3 models, User, Connection, Suspect
A User has many Connections,
A Connection has one Suspect, linked via case_id
A User has many Suspects through its Connections.
The code is as follows:
class User < ActiveRecord::Base
has_many :followe...
In my Rails application, I have two models, Articles and Projects, which are both associated with a user. I want to add comments to each of these models. What's the best way to structure this?
Here's my current setup:
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :project
end
class Article < ActiveRecord::Base
...