What's the best way to make 1 SQL call that checks if the join model already exists before we try to create another with this setup:
class Parent < ActiveRecord::Base
has_many :relationships, :as => :parent
has_many :children, :through => :relationships, :class_name => "Child"
end
class Child < ActiveRecord::Base
has_many :relati...
so i havent found much documentation about using conditions on activerecord find methods for associated models, but i have found a variety of examples. although none of them seem to be working for me.
user has_one avatar
avatar belongs_to user
Avatar.find(:all, :include => :user, :conditions => {:user => {:login => 'admin'}})
return...
I tried creating a DB migration in Rails that looked something like this:
ruby script/generate scaffold post user_id:int title:string content:text
Looking at the resulting .rb file, sure enough, I saw everything I'd entered:
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.int :user_id...
My database is empty, and I understand that I should get an exception.
Just want to make sure that my macbookpro is setup with rails properly.
typing:
user.find(1)
in console I get:
>> user.find(1)
NoMethodError: undefined method `find' for #<User:0x1016403a0>
from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.0/lib/active_model/a...
I have an ActiveRecord model with several virtual attribute setters. I want to build an object but not save it to the database. One setter must execute before the others. How to do?
As a workaround, I build the object in two steps
@other_model = @some_model.build_other_model
@other_model.setup(params[:other_model)
Where setup is:
cl...
this is a follow up to a previous question i asked. i am having trouble getting a query to work that has a variety of conditions for nested models.
user has_one avatar
user has_one profile
avatar belongs_to user
profile belongs_to user
and i am actually able to get this to work...
Avatar.find(:all, :include => {:user => :profile}, :...
I am tring to define a one to one relation between 2 tables using active record. I have a table Device and a table DeviceLocation. This is the code I use in DeviceLocation to define the relation:
[PrimaryKey(PrimaryKeyType.Foreign)]
public int DeviceID
{
get { return _deviceID; }
set { _deviceID =...
Let's say I have to simple models First and Second, and there is one to one relationship from Second using belongs_to :first. Now I want to do something with Second, when First is saved. But I don't want to setup an after_save callback in First, to deal with Second.
I want to keep my models clean, and unaware of each other as much as po...
Greetings,
I have an application where Companies and Users need to belong to each other through a CompanyMembership model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a boolean value admin). A simple version of the code:
class CompanyMembership < ActiveRec...
How do you eager load polymorphic has_many :through associations in Rails/ActiveRecord?
Here's the base setup:
class Post < ActiveRecord::Base
has_many :categorizations, :as => :categorizable
has_many :categories, :through => :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations, :as => :category
h...
I have a relationship in my ActiveRecord based model that looks like:
belongs_to :foo
My model should always have foo defined in it for it to be valid. My question is, when using validates_presence of, which one is the appropriate one to use:
validates_presence_of :foo
or
validates_presence_of :foo_id
Assuming here of course, th...
If I have something like this:
class Post < ActiveRecord::Base
has_many :comments, :as => :commentable do
def approved
find(:all, :conditions => {:approved => true})
end
end
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
...when I do this, I get 2 hits to the database (not...
In rails I have 2 tables:
bans(ban_id, admin_id)
ban_reasons(ban_reason_id, ban_id, reason_id)
I want to find all the bans for a certain admin where there is no record in the ban_reasons table. How can I do this in Rails without looping through all the ban records and filtering out all the ones with ban.ban_reasons.nil? I want to do t...
I want to find records in ActiveRecord that have an attribute that is either nil or some value:
class Model < ActiveRecord::Base
end
class CreateModels < ActiveRecord::Migration
def self.up
create_table :models do |t|
t.string :some_property
end
end
def self.down
drop_table :models
end
end
Model.all(:co...
I'm writing an Adhearsion component that uses ActiveRecord. The problem is that the component may be running for several minutes (the length of a call). During that time the component has an ActiveRecord object as an instance variable. This object uses up one database connection from the connection pool. Depending on the number of caller...
How can I use ActiveRecord's accepts_nested_attributes_for helper in a has_many :through association while adding attributes to the join table?
For example, say I've got a Team model:
class Team < ActiveRecord::Base
role = Role.find_by_name('player')
has_many :players,
:through => :interactions,
:source...
Hey,
I'm trying to save a hash of options in a single DB field. The form is able to save the data to the DB but not able to retrieve it again when I go to edit it (e.g. all the other fields are prepopulated except for the wp_options fields).
class Profile < ActiveRecord::Base
serialize :wp_options
end
This is my custom class:
...
I have two models:
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
I want to find all parents AND their children, with conditions on the children only. BUT if the parent has no children that match that criteria, I still want the parent.
I tried this:
Parent.all...
By default, ActiveRecord takes all fields from the corresponding database table and creates public attributes for all of them.
I think that it's reasonable not to make all attributes in a model public. Even more, exposing attributes that are meant for internal use clutters the model's interface and violates the incapsulation principle.
...
Greetings, all,
I'm working on an application in Ruby on Rails where we need to keep track of a bunch of external services for each user (for example, Facebook, MySpace, Google, SalesForce, Twitter, WordPress, etc) that the app will access on behalf of the user. For some services, we will need to store an (encrypted) username and passwo...