When working with a polymorphic association, is it possible to run an include on submodels that are only present in some types?
Example:
class Container
belongs_to :contents, :polymorphic => true
end
class Food
has_one :container
belongs_to :expiration
end
class Things
has_one :container
end
In the view I'm going to want to d...
I'm trying to create a record within a join table from the action of a button. To explain, I would have an events model and would like to track selected events from each user.
I used the HABTM relationship since I dont really need any extra fields.
User.rb => has_to_and_belongs_to_many :events Event.rb => has_to_and_belongs_to_many :us...
Hi all,
Need a little help with a SQL / ActiveRecord query. Let's say I have this:
Article < ActiveRecord::Base
has_many :comments
end
Comment < ActiveRecord::Base
belongs_to :article
end
Now I want to display a list of "Recently Discussed" articles - meaning I want to pull all articles and include the last comment that was adde...
I'm listing the data from one of my models in a table
<% @events.each do |event| %>
<tr>
<td align="center"><%= button_to "Add", :controller => 'personal', :action => "add" %> </td>
<td><%=h event.name %></td>
<td><%=h event.description %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', event %></td>
<% end %>...
I have a model that I'm trying to retrieve an array of data from, and I don't need the data to be instantiated into Ruby objects. In fact, that just introduces an extra step into my code to step through the objects and generate a new array with just the data I need.
Example:
class Book
#has attribute in database title
end
I would ...
There are currently two problems with storing ActiveRecord objects in memcached.
The Undefined Class/Module problem (Google search). From what I've read up, this is still a bug that nobody has a real good solution for. The cache_fu plugin has probably the best solution for this, wrapping its retrieve call in a block that tries to catc...
I have three models:
class Book < ActiveRecord::Base
has_many :collections
has_many :users, :through => :collections
end
class User < ActiveRecord::Base
has_many :collections
has_many :books, :through => :collections
end
class Collection < ActiveRecord::Base
belongs_to :book
belongs_to :user
end
I'm trying to display a l...
I have a RoR project on my Windows 7 PC.
I want to create some Ruby code that I can execute from the cmd.exe command line that manipulates the development database (via database.yml) of the project. (I don't want to have to run my utility code via a web page.)
What is the best way to go about pulling this off? (I'm a newbie.)
I can'...
I'm starting to get to grips with CodeIgniter and came across it's support for the Active Record pattern.
I like the fact that it generates the SQL code for you so essentially you can retrieve, update and insert data in to a database without tying your application to a specific database engine.
It makes simple queries very simple but ...
This code runs on my local RoR/Windows 7 (64-bit):
sql = ActiveRecord::Base.connection()
last_pk = sql.insert("insert into manual (name) values ('hello new value')")
puts 'last_pk=', last_pk
but always displays "0."
For various reasons I can't use ActiveRecord in this situation.
(Note: The above code runs fine on my shared host.
A...
I have three models:
class Address < ActiveRecord::Base
has_many :jobs
end
class Category < ActiveRecord::Base
has_many :jobs, :dependent => :destroy
end
class Job < ActiveRecord::Base
belongs_to :category
belongs_to :address
end
I'm trying to get all categories that the jobs has address_id != NULL
I'm almost there doing:
cate...
I have a set of columns that applies to more than 1 model.
For the sake of discussion, it could be:
User has many Addresses
Company has an Address
These are separate models, but the Address columns will be identical between the 2.
What's the best way to accomplish this in Rails?
...
I'm using the ancestry gem to structure some groups in a tree. At the same time I'm using acts_as_list to keep groups at the same tree level in a sorted list. Given the following model:
class Group < ActiveRecord::Base
acts_as_tree
acts_as_list :scope => "ancestry"
named_scope :parentable, :conditions => "NOT type = 'PriceGroup'"...
I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere).
For example, I can deal with all the attribute names:
ar.attribute_names.each { |name| puts name }
.attributes just returns a mapping of the names to thei...
I'm building a Rails app that has Etsy.com style functionality. In other words, it's like a mall. There are many buyers and many sellers.
I'm torn about how to model the sellers. Key facts:
There won't be many sellers. Perhaps less than 20 sellers in total.
There will be many buyers. Hopefully many thousands :)
I already have...
By default, the datetime field from the database is being converted and stripping off the milliseconds:
some_datetime => "2009-11-11T02:19:36Z"
attribute_before_type_cast('some_datetime') => "2009-11-11 02:19:36.145"
If I try to overrride the accessor for this attribute like;
def some_datetime
attribute_before_type_cast('some_datet...
If a model changes an attribute locally, then changes it back, ActiveRecord doesn't send the change to the DB. This is great for performance, but if something else changes the database, and I want to revert it to the original value, the change doesn't take:
model = Model.find(1)
model.update_attribute(:a, 1) # start it off at 1
# some ...
Let's say I have two models:
class Model1 < ActiveRecord::Base
has_many :model2
def save
self.attr = <sth. complex involving the associated model2 instances>
super
end
end
class Model2 < ActiveRecord::Base
belongs_to :model1
end
The statement in the overwritten save method will issue a complex query (using find [or a...
I have some projects. Those projects have users through memberships.
However, those users belong to companies. Question is, how do I find out which companies can access a project?
Ideally I'd be able to do project.users.companies, but that won't work.
Is there a nice, pleasant way of doing this?
...
So i am trying to do this
Order.find :all, :conditions => "org = 'test org'"
what ends up firing is
SELECT * FROM `orders` WHERE (org = 'test org')
the whitespace in the argument gets stripped. what am i missing.. im really stumped here. please help!
...