How to get all link entries except url columns that are empty? My controller looks like this:
def show
@user = User.find_by_username(params[:username])
@links = @user.links.all
respond_to do |format|
format.html
format.xml { render :xml => @links }
end
end
Thanks in advance!
Edit # 1
If I do this:
@links = @user.all(:conditions => 'url != NULL')
I get an error message:
NoMethodError in UsersController
undefined method `all' for #<User:0x2254f98>
And if I do this:
@links = @user.links.all(:conditions => 'url != NULL')
I still get all the links, even those that have empty url fields ...
Edit # 2
If i do
@links = User.all(:conditions => 'url != NULL')
I get
ActiveRecord::StatementInvalid in UsersController
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 that have empty url fields ...
I am wondering if the difference between NULL and field is not different?
Edit # 3
Now my code is as follows:
def show
@user = User.find_by_username(params[:username])
@links = @user.links.all(:conditions => "url <> ''")
respond_to do |format|
format.html
format.xml { render :xml => @links }
end
end
Decision
@links = @user.links.not_null_url
named_scope :not_null_url, :conditions => "url <> ''"
It works! Just make sure to access the links:
<% @links.each do |link| %>
And not:
#view
<% @user.links.each do |link| %>