try using a range in the condition...
Post.all(:conditions=>{:created_at=>Time.now.beginning_of_year..Time.now.end_of_year})

you can also create a named scope (rails 2.3) in the belongs_to end of
the relationship
class Post < ActiveRecord::Base
  named_scope :from_this_year, lambda { { :conditions=>{:created_at =>
Time.now.beginning_of_year..Time.now.end_of_year}}
end
...you need to use a lambda, otherwise the time value from app load is
used... which obviously wouldn't be what you want.

you would use it like so
User.first.posts.from_this_year

or you could set up a has_many
class User < ActiveRecord::Base
 
has_many :posts_from_this_year, :class_name=>"Post", 
:conditions=>{:created_at=>Time.now.beginning_of_year..Time.now.end_of_year}
end
@user.posts_from_this_year




On Apr 18, 1:30 am, Bob Smith <[email protected]> wrote:
> I'm trying to get records from a has_many that are only from this
> year.
>
>  has_many :visit, :conditions => "year = #[email protected]}", :dependent
> => :destroy
>
> This has trouble. it thinks @today is null.. Any other ideas?
>
> Bob
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to