On Apr 19, 5:24 am, bergonom <jbergh...@yahoo.com> wrote:
>
> When a user tries to add a new tag, I'm trying to detect if the
> content already has the tag.J
>
> So ... I grab the tag like this:
>      tag = Tag.where("name = ?", tag_name)
>
> Then I'm trying:
>      if !...@content.tags.where(:id => "#{tag.id}").present?
> but tag.id is always a huge number, like2165404200
>
Here tag is actually an activerecord::Relation, a proxy object that
will turn into an array if needed. It already has an I'd method though
so it sounds like you're getting back the ruby object id of the
relation rather than the database id of a tag

tag = Tag.where(...).first should work.

You could also do @content.tags.include?(tag) these days this will
either issue a query to check the presence of the single tag or, if
the association is already loaded, use the in memory array.

Fred
> I only have two tags in the db right now, so the ID should be either 1
> or 2
> and i've verified those IDs in the db.
>
> Any idea what's going on?
>
> Here's the whole block of code for reference:
>       params[:item][:tags].each do |tag_name|
>         tag = Tag.where("name = ?", tag_name)
>         if !...@content.tags.where(:id => "#{tag.id}").present?
>           @content.tags << tag
>         end
>       end

-- 
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to