How do I fetch all 'link' entries exept those with an 'url' column that is blank? My controller looks like this:
def show
@user = User.find_by_username(params[:username])
@links = @user.links.all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @links }
end
end
Thanks in advance!
Edit #1
If I do:
@links = @user.all(:conditions => 'url != NULL')
I get a error:
NoMethodError in UsersController#show
undefined method `all' for #<User:0x2254f98>
And if I do:
@links = @user.links.all(:conditions => 'url != NULL')
I still get all the links, even those with empty url fields...
Edit #2
If I do
@links = User.all(:conditions => 'url != NULL')
I get
ActiveRecord::StatementInvalid in UsersController#show
SQLite3::SQLException: no such column: url: SELECT * FROM "users" WHERE (url != NULL)
And if I do
@links = Link.all(:conditions => 'url != NULL')
I still get all the links, even those with empty url fields...
I'm wondering if it's a difference between NULL and the field beeing empty?
Edit #3
Now my code looks like this:
def show
@user = User.find_by_username(params[:username])
@links = @user.links.all(:conditions => "url <> ''")
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @links }
end
end
Solution
#controller
@links = @user.links.not_null_url
#model
named_scope :not_null_url, :conditions => "url <> ''"
This works! Just be sure access the links with:
#view
<% @links.each do |link| %>
And not:
#view
<% @user.links.each do |link| %>