Anyone have any idea why
named_scope :not_commented_by, lambda { |u| { :include
=> :comments, :conditions => ['comments.owner_id != ? OR comments.id
IS NULL', u.id] } }
this would effectively show comments from a user if they haven't commented
or if there are not comments but when they do comment and then someone else
comments the entry shows back up in the table again.
On Mon, Nov 9, 2009 at 8:57 PM, christian <[email protected]> wrote:
> I have implemented the named scoped method after carefully trying the other
> methods to get a better understanding.
>
> In the named scope method you use u which is short for users? I wasn't sure
> about the 'u' but I do understand the lambda and now better understand when
> to utilize a named scope.
>
> I am still having an issue with the not commented by portion, as it seems
> to to ignore it once another person has commented.
>
> Thanks for all your help.
>
>
> On Tue, Nov 10, 2009 at 3:29 AM, Matt Jones <[email protected]> wrote:
>
>>
>>
>> On Nov 9, 2009, at 9:36 PM, christian wrote:
>>
>> > When I try to use @user.id I get the following error
>> >
>> > "Called id for nil, which would mistakenly be 4 -- if you really
>> > wanted the id of nil, use object_id"
>> >
>> > Is there something else I would have to change in order for it to
>> > work without the object_id ?
>> >
>>
>> The problem is that you're looking at @user before it is set. Code
>> that needs the instance should be in the hobo_show block rather than
>> ahead of it.
>>
>> So your controller would look like
>>
>> def account
>> # @user is nil here
>> hobo_show do
>> # do something with @user here
>> end
>> end
>>
>> On the genre issue, if Genres aren't being regularly created, it's
>> better to keep them as an enum_string rather than cluttering things
>> with an extra association. Certainly, if you keep the Genre table, you
>> shouldn't *ever* specifically code a value for the id field - using
>> Genre.find_by_name('Prose') would be much more resilient to DB changes
>> and so on.
>>
>> With the genre table, one way to write the query you're looking for is:
>>
>> @prose = Genre.find_by_name('Prose').submissions.find(:all, :include
>> => :comments, :conditions => ['comments.owner_id != ? OR comments.id
>> IS NULL', @user.id])
>>
>> The "comments.id IS NULL" part is SQL's weird way of saying "there
>> isn't a record"; for instance, this will find all submissions with NO
>> comments:
>>
>> @no_comments_at_all = Submission.find(:all, :include
>> => :comments, :conditions => 'comments.id IS NULL')
>>
>> It looks really weird at first (since comments.id *can't* be null),
>> but is a result of the outer join that :include does; records in
>> 'submissions' that don't have any associated records joined against an
>> 'all null' row. comments.id in the above could actually be any field
>> that's guaranteed not to be null in real records, but the convention
>> is to use 'id' (since you've got other problems if you've got nil id
>> records...).
>>
>> As a final note, I'd still recommend that you roll a chunk of this
>> logic into named scopes, thus:
>>
>> class Submission
>> ...
>> named_scope :not_commented_by, lambda { |u| { :include
>> => :comments, :conditions => ['comments.owner_id != ? OR comments.id
>> IS NULL', u.id] } }
>> end
>>
>> This cleans up the @prose find to:
>>
>> @prose = Genre.find_by_name('Prose').submissions.not_commented_by(@user)
>>
>> Good named scopes end up reading almost like a sentence; even a Ruby
>> novice would have a good chance of figuring out what the statement
>> above does.
>>
>> Hope this helps!
>>
>> --Matt Jones
>>
>>
>>
>> >>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo
Users" 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/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---