I'm just starting out with Rails and ActiveRecord. I feel that anything I want to make an association between two models (belongs_to, has_one, has_many) I have to read the documentation over and over to really understand it.
Does anyone know of a good cheatsheet or refcard that makes the distinctions visible ?
...
I'm using rails3.rc and activerecord3 (with meta_where) and just started to switch to sequel, because it seems to be muuuuch faster and offers some really great features :-)
I'm already using the active_model plugin (and some others). But here are some questions I found and which I didn't find any documentation for:
As far as I know, ...
I am new to rails and read this guide to get all the info so far.
I have a simple scenario and want to make sure whether or not my associations will work fine.
Scenario:
User logs in -> sets up many groups -> each group has many employees
User model:
class User < ActiveRecord::Base
has_many :groups
end
Group model:
class Gro...
The way this program needs to work is like this:
A person can view an event and then choose to either register directly for an event or for a team (which is registered for an event).
class Event
has_many_polymorphs :registrants, :from => [:users, :teams], :through => :registrations
end
class Team
belongs_to :registrant, :polymorphi...
hi,
Course.find(:all, :group =>:id, :order => 'updated_at DESC', :joins=> :students :conditions => { :students => { :first_name=>"John", :status => 1}})
looking this query, passing the conditions as a hash, there is a way to:
construct a where :first_name not null?
construct a where :first_name != "John"?
...
I am working in Padrino and have this in my controller
@work.find_by_id(params[:id])
I want to add prev/next buttons into my view, and so must be able to get the path to the next item on the list. How can I do that with ActiveRecord?
...
Alright, Im trying to count all the rows where "Membership_Status" = Active. The result I get right now is "Array" instead of a number.
Here is my model
class Report_model extends Model
{
function count_members()
{
$query = $this->db->get_where('Membership', array('Membership_Status' => 'Active'));
return $query->num_rows();
}
...
I want to implement a sql query using CodeIgniter Active Record class. The query looks like this..
INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = 'CA'
Is this possible in CodeIgniter without using the $this->db->query method?
Solution:
$this->db->select('au_id, au_ln...
Hi folks!
I have a CodeIgniter/PHP Model and I want to insert some data into the database.
However, I have this set in my 'raw' SQL query:
ON DUPLICATE KEY UPDATE duplicate=duplicate+1
I am using CodeIgniter and am converting all my previous in-controller SQL queries to ActiveRecord. Is there any way to do this from within the Activ...
I came across this idiosyncrasy while testing my validations. With a migration defined as follows:
create_table :time_windows do |t|
t.datetime :window_begin, :null => true
t.datetime :window_end, :null => true
end
in irb
>> t = TimeWindow.new({:window_begin => Time.now, :window_end => "not a time"})
=> #<TimeWindow id: nil, win...
Hi. In a Rails app I have a Company ActiveRecord Object and one of its field is a :text. This app must to work (please don't ask why... is my boss decision) in mysql and Oracle. in mysql works without problem (is a text datatype), but in Oracle is a CLOB and every time I tried to save (a new object or updated a existing one), this error ...
I'm working on a rails project for an Australian website. As a result, they want to be able to enter date formats in the more european-standard of 'dd/mm/yyyy' rather than the US-centric 'mm/dd/yyyy'. I have an ActiveRecord model with a Date field. I'm using jQuery's datepicker to provide the date select on a text field, and have it s...
Hello,
I have this find condition pulling from my model that currently looks like this.
@major_set = Interest.find(:all, :conditions => {:id => 1..21})
I'd like to add some more individual ids that I've just added which are like 120...130. I tried to do...
@major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
...
For example, posts table is in db1, categories table and category_post_join table are in db2. How to declare a many-to-many relationship between Post model and Category model?
'categories'=>array(self::MANY MANY, 'Category', 'category_post_join (post_id, category_id)')
works only if all tables are in the same database.
...
Alright Im trying to retrive the number of rows where members are individual and active.
Ive narrowed the problem to my model. Everything was working fine until I added a second clause to my get_where
Here is my model function
function count_individual_active_members()
{
$query = $this->db->get_where('Membership', arr...
I know that you can do something like this to load the rails environment:
task :my_task => :environment do
MyModel.find(1)
end
But it seems the code in the models are not executed. I am using acts_as_audited, and there is a nice class function which retrieves all models which are being audited. The call looks something like:
...
I execute the following commands to make the model:
script/generate model user firstname:string lastname:string
script/generate song group songname:string songtitle:string
a user has_many :songs and a song belongs_to :user
after this I run rake db:migrate however, the associations are not carried to my actual DB. Because in my actu...
I have events and users/teams.
class Event
has_many :users, :through => :registrations
end
class User
has_many :events, :through => :registrations
end
class Registration
belongs_to :users
belongs_to :events
end
When I register a user, I'm connecting them to the event like so:
@event.users << @user
Does this implicitly creat...
Hi,
I am trying to display a table which contains 8500 records, I am using the same controller/model functions as I have used throughout the site which all work fine.
On this page however, I just see a blank screen. Is this a known Issue with codeigniter? Is there a work around? I am totally stumped, my only option I guess is to split ...
Hi Everyone,
I have 2 models:
class Video < ActiveRecord::Base
belongs_to :categories, :foreign_key => "category", :class_name => "Category"
end
class Category < ActiveRecord::Base
has_many :videos
end
This is fine so far, in my videos controller for the index page I have:
def index
@videos = Video.all(:joins => :categories)
etc, ...