I have a model called Profile which is belong_to User, so there is 'user_id' for the database to keep track of. In the local admin interface I made for this model I wanted to provide the flexibility of allowing admin to enter an username to a field in the editing screen, and then resolve that to user_id for saving in controller.
However...
I'm following this tutorial which is working splendidly for has_many :through relationships. I've got normal things like category_product working.
However, I am not able to conceptualize this situation (nor get it to work): I have a Category that has related Categories. Since every category can have N categories... first off, is this ac...
Ok, so everyone has decided (and for good reason) strait SQL is of the devil. This leaves us with many methods of placing a "middle-man" in our code to separate our code from the database. I am now going to spit out all the info I have gathered in the hope someone can set me strait and tell me what I built.
An ORM (Object-relational map...
I have been reading up on multiple PHP frameworks, especially the Zend Framework but I am getting confused about the proper way to go forward.
Zend Framework does not use ActiveRecords but instead uses the Table Data Gateway and Row Data Gateway pattern, and uses a DataMapper to map the contents of the Row Data Gateway to the model, bec...
This is a common question, but the explanations found so far and observed behaviour are some way apart.
We have want the following nHbernate strategy in our MVC website:
A SessionScope for the request (to track changes)
A ActiveRecord.TransacitonScope to wrap our inserts only (to enable rollback/commit of batch)
Selects to be outside ...
Assuming I have 5 tables. Can ActiveRecord handle this? How would you set it up?
The hierarchy:
Account (Abstract)
CorporateCustomer (Abstract)
PrivateCustomer
PublicCustomer
GovernmentCustomer
Edit: In nhibernate and castle activerecord the method needed to enable this scenario is called "joined-subclasses".
...
I am trying to do this
Version.find(:all, :joins=>"JOIN editions ON versions.edition_id=editions.id JOIN products ON editions.product_id=products.id", :select=>"products.name, versions.name AS what")
but ActiveRecord is not respecting the AS keyword... any ideas?
Edit: Since both fields are called "name" they are colliding, so I only...
I have an ActiveRecord model Language, with columns id and short_code (there are other columns, but they are not relevant to this question). I want to create a method that will be given a list of short codes, and return a list of IDs. I do not care about associations, I just need to end up with an array that looks like [1, 2, 3, ...].
M...
Hey guys, my first question here.
I am making a site in RoR and I am inside a multi DB environement. By that, I mean that some of my models are linked to MSSQL tables, and some others are linked to MYSQL tables.
It works well for the most part, but when I use the 'include' option in a find method, I get a very weird mix of SQL. Let me ...
Hello, I just wanted to get some feedback on better ways to model a team/team membership in rails.
I currently have the following:
class User
has_many :teams, :foreign_key => "owner_id" #user owns this team
has_many :memberships #user is a member of these teams
class Team
belongs_to :team_administrator, :class_name => "Use...
Part of why I love Rails is that I hate SQL - I think it's more like an assembly language that should be manipulated with higher level tools such as ActiveRecord. I seem to have hit the limits of this approach, however, and I'm out of my depth with the SQL.
I have a complex model with lots of sub-records. I also have a set 30-40 named...
I have an Order class that has_many :shipments. How can I use Order.find to return all the order objects whose newest shipment was created after a certain time (say, in the last hour)?
...
This code in my view triggers an error:
<% remote_form_for(mymodel) do |f| %>
<%= f.error_messages %>
(mymodel is not an ActiveRecord object)
When I look at the error trace, I see this section which indicates that error_messages in the view translates into error_messages_for in the active_record_helper:
C:/Ruby18/lib/ruby/gems/1.8...
I was trying to understand this call:
deprecate :new_record?, :new?
which uses this deprecate method:
def deprecate(old_method, new_method)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{old_method}(*args, &block)
warn "\#{self.class}##{old_method} is deprecated," +
"use \#{self.class}##{...
Hi. I have a Model Book with a virtual attribute for create a Editor from the Book form.
The code looks like:
class Book < ActiveRecord::Base
has_many :book_under_tags
has_many :tags, :through => :book_under_tags
has_one :editorial
has_many :written_by
has_many :authors, :through => :written_by
def editorial_string
self...
I have two scaffold-generated models: Book and Bookbag. A Bookbag has-many Books, and a Book belongs-to a Bookbag. Each Book has a weight, and each Bookbag has an average-weight that is supposed to store the average weight of all of its Books. What is the best way to keep average-weight up to date?
Using a before-save filter on Bookba...
Assume we have an usual M-M relationship between two tables, like:
users---< users_tags >--- tags.
In this post I'm only concerned about the relation user_tags, tags: I'd like to avoid that linked tags can be deleted. Only tags which aren't referenced should be destroyable.
The stupid way to do this would be:
class Tag
def before...
Is it possible in ruby/rails to order a result set after doing a find? For example, is it possible to do something like this
Warning: Does not work
if type == 1
@items = mycompany.items
else
@items = myhome.items
end
@items = @items :order => "created_at"
I would assume something like this should be possible, but I'm still very...
I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within a UsersController and a ProjectsController.
Resource_controller fails when it attempts to pull the release from User, but succeeds from Project. The problem under User is finding the Release results in an object of type, Enumerable::Enumera...
I'm trying to implement a social networking style friendship model and I didnt have much much luck trying to figure out the plugins available out there. I think I'll learn Rails better if I do it myself. So here's what I have :
class User < ActiveRecord::Base
has_many :invitee_friendships ,
:foreign_key => :friend_id,
...